Skip to content
Advertisement

How to to JOIN 2 tables with foreign key pointing to other table primary key to return all the row that have such primary key in SQL?

Please help I need to get rows in the 2 tables with the foreign key poiting to table_a as seen on picture. I have tried Joining this way,

SELECT * FROM table_a 
INNER JOIN table_b
ON table_a.id = table_b.my_col
INNER JOIN table_c
ON table_a.id = table_c.my_col



but it return empty results. Please help me to fix such SQL JOIN statement

PLEASE LOOK AT THE PHOTO I MADE WHICH SHOW THESE TABLES AND MORE ELABORATION

enter image description here

Advertisement

Answer

could be that you have not valid match in table_b and table_c (your id for table_a don’t match values in table_b and table_c)

try use left join

SELECT * 
FROM table_a 
LEFT  JOIN table_b ON table_a.id = table_b.my_col
LEFT  JOIN table_c ON table_a.id = table_c.my_col
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement