I have two tables say named table_1 and table_2. The schema is somewhat as follows.
table_1
+----+--------+-----+------------+
| id | reg_no | ... | table_2_id |
+----+--------+-----+------------+
table_2
+----+-----+
| id | ... |
+----+-----+
The column table_2_id in table_1 refers to the column id in table_2. Now, I have to get the table_2_id for a specific reg_no and then use that table_2_id to get data from table_2.
I currently do it as follows and it works.
$stmt = $this->conn->prepare("SELECT table_2_id from table_1 WHERE reg_no = ?");
$stmt->bind_param("s", $reg_no);
$stmt->execute();
$stmt->bind_result($table_2_id);
$stmt->fetch();
$stmt->close();
$stmt = $this->conn->prepare("SELECT * from table_1 WHERE id = ?");
$stmt->bind_param("i", $table_2_id);
$stmt->execute();
...
Is this the correct way to do it? Is there some other more efficient query to perform this task?
The query using join
SELECT t2.*
FROM table_1 t1
JOIN table_2 t2 ON t1.table_2_id = t2.id
WHERE t2.t1reg_no = ?
It should be more efficient to do this with one query like this:
SELECT table_2.*
FROM table_1 LEFT JOIN table_2 ON table_1.table_2_id=table_2.id
WHERE table_1.reg_no = ?
table_1 LEFT JOIN table_2 ON table_1.table_2_id=table_2.id will join table_1 and table_2.
More specifically it will create a table which will have all columns from both table_1 and table_2
and then it will put in it all rows from table_1. Also when there is a row in table_2 with id same as the
table_2_id it will also fill the tables_2's columns.
The above action will be limited to only those rows where table_1.reg_no = ?
Finally with SELECT table_2.* we get only the columns from the table_2 as in your example.
Try
SELECT T2.* FROM table_2 T2
LEFT JOIN table_1 T1 ON T1.table_2_id = T2.id
WHERE T1.reg_no = ?;
Example
table_1
id reg_no table_2_id
1 001 1
2 002 2
3 003 4
4 004 7
5 005 8
table_2
id column_test
1 A
2 B
3 C
4 D
5 E
6 F
7 G
Query
SELECT T2.* FROM table_2 T2
LEFT JOIN table_1 T1 ON T1.table_2_id = T2.id
WHERE T1.reg_no = 003;
Output
id column_test
4 D
Related
I have 5 tables that I need to connect to get the necessary data.
Table1
id | number
1 | 1
2 | 5
Table 2
id | number | user_id
1 | 1 | 9
2 | 5 | 8
Table 3
id | name |
8 | john |
9 | jane |
Table 4
id | email
6 | johndoe#example.com
Table 5
id | table4_id | table3_id
1 | 6 | 8
Table 1 is my main table and I want to add the name and email from table 3 and 4 respectively to my select query of table 1, but in order to do so, I would need to use table 2 and 5 to connect them as there is no direct relationship between table 1 and table 3 and 4. I only know how to join 3 tables and not 5 tables and it seems confusing to me on how to proceed with this.
I followed the link here to join Table 1,2 and 3. But I don't know how to proceed with table 4 and 5.
This is the query I tried:
SELECT table1.number, table2.number, table2.user_id, table3.id, table3.name,
table4.id, table4.email, table5.table4_id, table3_id
FROM table1
LEFT JOIN table2
INNER JOIN table3
//this query will work if I don't include this 2 inner joins
INNER JOIN table4
INNER JOIN table5
ON table3.id = table5.table3_id
ON table5.table4_id = table4.id
//
ON table2.user_id= table3.id
ON table2.number = table1.number;
ERROR: (if included the inner join for table 4 and 5)
Error Code: 1064. You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near 'ON table2.user_id= table3.id
The right Syntax is select .. from .. join .. on .. join ..on ....
SELECT table1.number, table2.number, table2.user_id, table3.id, table3.name, table4.id, table4.email, table5.table4_id, table3_id
FROM table1
LEFT JOIN table2 ON table2.number = table1.number
INNER JOIN table3 ON table2.user_id= table3.id
INNER JOIN table5 ON table3.id = table5.table3_id
INNER JOIN table4 ON table5.table4_id = table4.id
Try with the below structure. I made it from the structure you given in question
select t1.number, t2.number, t2.user_id, t3.id, t3.name,t4.id, t4.email, t5.t4_id, t3_id
from table5 as t5
join table4 as t4 on t5.table4_id = t4.id
join table3 as t3 on t5.table3_id=t3.id
join table2 as t2 on t2.user_id = t3.id
join table1 as t1 on t2.number=t1.number
Try Below code. Hope this will work.
Select t3.name,t4.email,t1.number,t2.user_id
from table3 t3 JOIN table5 t5 ON t3.id=t5.table3_id
JOIN table4 t4 ON t4.id=t5.table4_id
JOIN table2 t2 ON t2.user_id=t3.id
JOIN table1 t1 ON t1.number=t2.number;
I have 2 tables as follows:
table1
ID Name Test
A011 John 1
A012 Lynda 1
A013 Micheal 1
A014 Jack 0
A021 Joe 1
A015 Paul 0
table2
ID Done
A011 1
A012 1
I want to select all rows from table1 that have an ID where the 3 first letters are equal to A01, and the test field is 1, and also the ID is not present in table2.
I tried this query:
SELECT a.* FROM table1 a LEFT JOIN table2 b ON a.ID = b.ID
WHERE a.test = 1 AND b.ID IS NULL
The result from that is 2 rows with ID A013 and A021. I tried to use LEFT(ID,3) to get the ID with A01, however, I couldn't achieve what I want.
How can I filter only the records where the ID starts with A01?
Try this, it will give you the desired result
SELECT t1.* FROM table1 t1 LEFT JOIN table2 t2 ON t1.userid = t2.userid WHERE LEFT(t1.userid , 3) LIKE '%A01%' AND t1.userid NOT IN (SELECT userid from table2)
SELECT * FROM table1
WHERE test = 1
AND ID LIKE "AO1%"
AND ID NOT IN (SELECT ID from table2)
table1
--------------
| sn | class |
--------------
table2
----------------
| id | student |
----------------
all are int as sn is table1 is linked to student in table2
sn, id are auto increasing. when inserting data to table2 student column is same as sn in table 1
now I want to select student in table2 but only those whose class in table1 is "3"
my syntax is thus;
$count = mysql_query(SELECT student from table2 whose class in table1 =3)
so that i can count them by saying
$quantity = mysql_num_rows($count)
now my problem is if sql also have this whose keyword, or how do i go about this.
$count = mysql_query(SELECT student from table2 whose class in table1 =3)
You need to join the tables in order to filter the results properly.
(1) This will give you the number of students for class 3.
$count = mysql_query(
'SELECT COUNT(t2.student)
FROM table2 t2
INNER JOIN table1 t1
ON t1.sn = t2.student
AND t1.class = 3'
);
(2) This will give you all classes and the number of students for each.
$count = mysql_query(
'SELECT t1.class, COUNT(t2.student)
FROM table2 t2
INNER JOIN table1 t1
ON t1.sn = t2.student
GROUP BY t1.class
ORDER BY t1.class'
);
(3) This will give you all classes and the students list.
$list = mysql_query(
'SELECT t1.class, GROUP_CONCAT(t2.student SEPARATOR ',')
FROM table2 t2
INNER JOIN table1 t1
ON t1.sn = t2.student
GROUP BY t1.class
ORDER BY t1.class'
);
You should join those two tables and limit your result to those which have table1.class = 3
SELECT
student
FROM
table2 a
JOIN table1 b ON (a.student = b.sn)
WHERE b.class = 3
If you want a count you could also do it through SQL by using aggregate function
SELECT
COUNT(student)
FROM
table2 a
JOIN table1 b ON (a.student = b.sn)
WHERE b.class = 3
Each table (table1 & table2) has its own DATETIME field.
I'm trying to catch id's of the two tables and order them by their DATETIME field.
Example:
Table 1 Table 2
------------ -------------
id | datetime1 id | table1id | datetime2
------------------------ -----------------------
1 | 2014-09-21 20:31:26 1 | 2 | 2014-09-21 20:31:29
2 | 2014-09-21 20:31:27 2 | 3 | 2014-09-21 20:31:30
3 | 2014-09-21 20:31:28
Table 3
------------
id | user
------------------------
2 | phil
3 | nathalie
My output isn't ordered properly with this query:
SELECT *
FROM (
SELECT
1 AS selection,
table1.id, table1.datetime1,
table2.datetime2
table3.user
FROM Table1
LEFT OUTER JOIN table2
ON table1.id = table2.table1id
LEFT OUTER JOIN table3
ON table1.id = table3.id
UNION ALL
SELECT
2 AS selection,
table1.id, table1.datetime1,
table2.datetime2
table3.user
FROM Table1
INNER JOIN table2
ON table1.id = table2.table1id
INNER JOIN table3
ON table1.id = table3.id
) AS query
ORDER BY table1.datetime1 DESC, table2.datetime2 DESC
Desired data:
from table 2 id: 2, 1,
from table 1 id: 3, 2, 1
So: 2, 1, 3, 2, 1
////EDIT
To people who could be struggling with long and complex MySQL request, please try it in PhpmyAdmin! It will tell you the error!
////EDIT
What you really need to do is to consider your schema more carefully. Consider naming the date time columns the same and then running a query like this - http://sqlfiddle.com/#!2/a3b4c/7/0
SELECT selection, id, datetimefoo, user FROM (
SELECT
1 AS selection,
table1.id, table1.datetimefoo,
table3.user
FROM table1
LEFT OUTER JOIN table2
ON table1.id = table2.table1id
LEFT OUTER JOIN table3
ON table1.id = table3.id
UNION
SELECT
2 AS selection,
table1.id, table1.datetimefoo,
table3.user
FROM table1
INNER JOIN table2
ON table1.id = table2.table1id
INNER JOIN table3
ON table1.id = table3.id
) AS T2
ORDER BY datetimefoo DESC
In the SQL fiddle this produces the results closer to what you're looking for. I am still not sure why you need the INNER JOINS on the second query though - there is nothing that you're doing here whcih requires them.
Here is another method that does not require a changing of the column names, but requires an alias for the sortable columns - http://sqlfiddle.com/#!2/ec4bc/3/0
SELECT * FROM (
SELECT
1 AS selection,
table1.id, table1.datetimefoo AS sort_date, -- alias on first table's date
table2.datetimebar,
table3.user
FROM table1
LEFT OUTER JOIN table2
ON table1.id = table2.table1id
LEFT OUTER JOIN table3
ON table1.id = table3.id
UNION
SELECT
2 AS selection,
table1.id, table1.datetimefoo,
table2.datetimebar AS sort_date, -- alias on second table's date
table3.user
FROM table1
INNER JOIN table2
ON table1.id = table2.table1id
INNER JOIN table3
ON table1.id = table3.id
) AS T2
ORDER BY sort_date DESC
I believe you are over-complicating a rather straight-forward task:
SELECT *
FROM (
SELECT 1 AS selection, table1.id as id, table1.datetime1 as date FROM Table1
UNION ALL
SELECT 2 AS selection, table2.id as id, table2.datetime2 as date FROM Table2
) AS query
ORDER BY date DESC
I have multiple tables in my database. Let's say the table users looks like this:
Users:
|id|name|gender|access|id_ext|
|1 | a | m | 1 | 32 |
|3 | b | m | 3 | 33 |
|4 | c | m | 1 | 34 |
|5 | d | f | 1 | 35 |
I would like to select the user with for example id_ext = 32 and then run another select statement using that selected users fields.
I can solve this by first getting the user with a query and then create another query with users info, but there must be a way to do this in the same query?
This is the query i use now:
SELECT * FROM users NATURAL JOIN
(SELECT id FROM ages WHERE age BETWEEN
(SELECT limit_age_l FROM users WHERE id=17)
AND (SELECT limit_age_h FROM users WHERE id=17)) as a
WHERE NOT id = 17
AND locale = 'en_US'
AND limit_gender = 1
AND visible = 0
AND NOT EXISTS (SELECT view_id FROM matches WHERE user_id = 17 AND view_id = a.id)
LIMIT 1
Problem is that the values id=17, limit_gender=1 and locale = 'en_US' in the query are not known. These are taken from the user with id_ext = '32'.
SELECT * FROM Users WHERE id in (SELECT id FROM Users WHERE id_ext='32');
Yes - assuming your subsequnt query is of the form:
select field1, field2, ...
from Table1
join Table2 on ...
where ...
and Table1.id = N /* previously selected id from users */
Then either by using the first query as a subquery:
select field1, field2, ...
from Table1
join Table2 on ...
where ...
and Table1.id = (select id from users where id_ext ='32')
/* replace = with IN if more than one id will be returned */
Or by joining to the results of the first query as part of the subsequent query:
select field1, field2, ...
from users
join Table1 on Table1.id = users.id
join Table2 on ...
where ...
and users.id_ext ='32'
(Note that both of these forms assume that users is not already being joined in the existing query - if it is, just add the users.id_ext ='32' condition to the existing query.)
EDIT: If I have understood the requirements correctly, the required query could be written as:
SELECT u.*
FROM users u
join ages a on u.id = a.id and
u.age between limit_age_l and limit_age_h
join users ul on ul.id = 17 and
ul.id <> u.id and
ul.locale = u.locale and
ul.limit_gender = u.limit_gender and
ul.visible = u.visible
AND NOT EXISTS (SELECT NULL
FROM matches m
WHERE m.user_id = ul.user_id AND m.view_id = a.id)
LIMIT 1
SELECT * FROM users WHERE id = (SELECT id FROM users WHERE id_ext = '32');
Select * from users as user inner join userinfo as usinfo on usinfo.id=user.id_ext where user.id_ext='32'