user965748
user965748

Reputation: 2387

Multiple tables Mysql JOIN

I have a reference table (main) of products names and a few other tables with alternative names. At this moment I have 2 tables of alternative names and I display those rows where a FK to the reference table from the table A exist only using

SELECT main.id,main.name,tabA.name,tabB.name FROM main INNER JOIN tabA ON tabA.fk=main.id LEFT JOIN tabB ON tabB.fk=main.id ORDER BY main.name

How to get all rows where a FK exist from any of the alternative tables?

Upvotes: 0

Views: 234

Answers (1)

Eugen Rieck
Eugen Rieck

Reputation: 65264

SELECT main.id,main.name,tabA.name,tabB.name 
FROM main 
LEFT JOIN tabA ON tabA.fk=main.id 
LEFT JOIN tabB ON tabB.fk=main.id 
ORDER BY main.name

Upvotes: 1

Related Questions