This question already has answers here:
SQL Inner-join with 3 tables?
(12 answers)
Closed 5 years ago.
I have a problem ...
In table1 I have an id, I have to compare that id in table2, then fetch the second id that is in table2 and compare it to table3 and get as a result a datum.
Example
TABLE1
ID NAME ECC...
1 Jhon
2 Frank
TABLE2
ID ID2 ECC..
1 4
2 8
TABLE3
ID NAME
4 Sea
8 Hello
If I look for id 1, the result must be Sea
If I look for id 2 the result must be Hello
Thanks!
SELECT Table3.NAME
FROM Table1
INNER JOIN Table2
ON Table1.ID = Table2.ID
INNER JOIN Table3
ON Table3.ID = Table2.ID2
WHERE Table1.ID = 1 -- Your Search here
You should use joins.
Your query will look like :
SELECT t3.name
FROM table3 t3
LEFT JOIN table2 t2 ON t3.id = t2.id2
LEFT JOIN table1 t1 ON t2.id2 = t1.id
WHERE t1.id = <your_number>
select Table2.Id,Table3.Name from Table1 inner join Table2 on Table2.ID2 = Table3.Id
Query:
SELECT a.name As Name FROM table3 a JOIN
(SELECT b.id2 AS id FROM table1 a JOIN table2 b ON a.id = b.id)b
ON a.id = b.id where b.id = <your id number (1,2) Anything>
Should be something like this:
select table3.name as name3
from table3
where table3.ID = table2.ID2
and table2.ID = table1.ID
and table1.ID = <YOURNUMBERHERE>
Related
This query select all from my first table where the row id doesnt exist in my second table:
SELECT *
FROM table1
WHERE NOT EXISTS (SELECT idTable1 FROM table2 WHERE table1.id=idTable1
This query select all row of table1 who exists in the table2 with the last id of table2 ( cause one row of table1 can have multiple row of the table2 but only the last is not forget ):
SELECT *
FROM table2
INNER JOIN table 1 ON idTable1 = table1.id
WHERE table2.id IN (SELECT MAX(actioncur.id) FROM table2 GROUP BY idTable1)
I want to regroup the both query in one, I want to select all row of table1 when id doesnt exist in table2 and select all of the table1 for the last table2 id. For exemple i want to select that : row 1 -> id=44 ; table2.id= 187 ; idTable1=44. Row 2 ->id=45 ; table2.id=? ; idTable1=?
If I understand correctly, you can use a correlated subquery:
SELECT t1.*,
(SELECT MAX(t2.id)
FROM table2 t2
WHERE t1.id = t2.idTable1
) as max_t2id
FROM table1 t1;
You can also do this with LEFT JOIN and GROUP BY:
SELECT t1.*, MAX(t2.id) as max_t2id
FROM table1 t1 JOIN
table2 t2
ON t1.id = t2.idTable1
GROUP BY t1.id;
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
Here is my tables structure.
Table1 // it has 4 colums,that saves all the question posted by user
id|question|answer|postby|
Table2 //it has 2 colums, that saves all the signed up users
id|username|email|
Table3 //it saves that which question is read by which user.
id|reader_id|question_id|
NOTE:reader_id is the id of table 2.
Problem: We need to find out those questions that are not being read by a particular user.
select * from table1 where id not in (
select question_id from table3 where reader_id = [particular user id]
)
SELECT question FROM table1 where id NOT IN (SELECT question_id FROM table3 where reader_id IN (SELECT id FROM table2 where username='XXXX'))
Try this:
SELECT t1.*
FROM table1 t1
LEFT JOIN
(SELECT t3.*
FROM
table2 t2
JOIN table3 t3 ON t3.reader_id=t2.id
WHERE t3.reader_id=SOME USER)t ON t.question_id=t1.id
WHERE t3.id IS NULL;
I have 2 tables
Table1 - columns:
User1Id, User2Id, User3ID
Table2 - columns:
UserID, Name, Family
How can get Name and Family from User1Id, User2Id, User3ID?
select t1.user1id, t1.user2id, t1.user3id, t1.date, t1.comment
t2.name as name1, t2.family as family1,
t3.name as name2, t3.family as family2,
t4.name as name3, t4.family as family3
from table1 t1
left join table2 t2 on t1.user1id = t2.userid
left join table2 t3 on t1.user2id = t3.userid
left join table2 t4 on t1.user3id = t4.userid
SELECT Name, Family FROM Table1, Table2 WHERE Table2.UserID = Table1.User1Id
UNION
SELECT Name, Family FROM Table1, Table2 WHERE Table2.UserID = Table1.User2Id
UNION
SELECT Name, Family FROM Table1, Table2 WHERE Table2.UserID = Table1.User3ID
I have drawn this image to explain what I need
1.to compare a user_id with the user_id's in two different tables
2.the corresponding ref_global_id from both the tables are then matched to a events table
3.matching global_id's from the events table are then arranged in ascending order.
Or this:
SELECT e.global_id, e.event_time
FROM (SELECT * FROM table1
UNION
SELECT * FROM table2) x inner join
event_table e ON e.global_id = x.ref_global_id
WHERE x.[user_id] = 121
SELECT e.global_id, e.event_time
FROM events_table e
JOIN table1 t1 on e.global_id = t1.ref_global_id
JOIN table2 t2 on e.global_id = t2.ref_global_id
WHERE t1.user_id = 121 AND t2.user_id = 121
ORDER BY e.event_time
Try this:
select global_id, event_time from event left join table1 on event.global_id = table1.ref_global_id AND table1.user_id = 121 left join table2 on event.global_id = table2.ref_global_id AND table2.user_id = 121