tslid
tslid

Reputation: 345

MySql inner join of 2 tables

Im sure Im missing something vital here so any advices are welcome. I have one base table lets call it Content_1 and two additional tables called Content_2 and Content_3. What Im trying to do is get all results from table_2 matching the id from table 1 and to add to this result set all results from table_3 which also match the id coming from table_1. Basically to have an OR condition in the final results - return everything from table 2 matching the id from table 1 OR return everything from table 3 matching the id from table 1. However I see that no results are returned so my guess is that we make the first join and then second join is applied to the result set returned after the first join, not on the initial join.

SELECT * FROM Content_1
JOIN Content_2 ON Content_1.id = Content_2.id
JOIN Content_3 ON Content_1.id = Content_3.id

Upvotes: 0

Views: 45

Answers (1)

Eric
Eric

Reputation: 3257

You probably want UNION instead of joining all 3 tables.

SELECT Col1, Col2 
FROM Content_1
JOIN Content_2 ON Content_1.id = Content_2.id
UNION
SELECT Col1, Col2
FROM Content_1
JOIN Content_3 ON Content_1.id = Content_3.id

Upvotes: 1

Related Questions