select table1.id,table1.value,table2.id,table2.value from table1 left join merged on table1.id=merged.tabid1 left join table2 on merged.tabid2=table2.id union select table1.id,table1.value,table2.id,table2.value from table2 left join merged on table2.id=merged.tabid2 left join table1 on merged.tabid1=table1.id ;
or with the right to join:
SELECT * FROM table1 LEFT JOIN merged on table1.id=merged.tabid1 LEFT JOIN table2 ON merged.tabid2=table2.id UNION SELECT * FROM table1 RIGHT JOIN merged on table1.id=merged.tabid1 RIGHT JOIN table2 ON merged.tabid2=table2.id WHERE table1.id IS NULL;
source share