Join 1 table on 2 different tables

Post Reply
mister_v
Posts: 188
Joined: Thu Mar 04, 2010 9:19 pm

Join 1 table on 2 different tables

Post by mister_v »

Hi,

how do I make a join of 1 table on 2 different tables with the same identifier ?

Table1 : id, other_id;
table2 : id, name
table3 : id, title

So table 1 on table2 and on table 3 with other_id to id.
chris
Site Admin
Posts: 194
Joined: Mon Jul 21, 2008 9:52 am

Re: Join 1 table on 2 different tables

Post by chris »

You can work with a sub-query an UNION:

Code: Select all

SELECT table1.id,union_table.name FROM table1 LEFT JOIN 
   (SELECT id,name FROM table2 UNION SELECT id,title AS name FROM table3) 
   AS union_table ON table1.other_id=union_table.id;
Post Reply