how do i select the required attributes from a union of left join and right join
I have two tables on which I am unable to p开发者_JAVA百科erform full outer join so I am doing a union of left and right outer join. each table has attributes like
tableA - one,two,three,four
tableB - one,two,three,four
(SELECT * FROM tableA
LEFT OUTER JOIN tableB ON
tableA.two=tableB.two)
UNION
(SELECT * FROM tableA
RIGHT OUTER JOIN tableB ON
tableA.two=tableB.two)
You just need to alias the tables
SELECT a1.one, a1.two, b1.one, b1.two
FROM tableA a1
LEFT OUTER JOIN tableB b1
ON a1.two=b1.two
UNION
SELECT a2.one, a2.two, b2.one, b2.two
FROM tableA a2
RIGHT OUTER JOIN tableB b2
ON a2.two=b2.two;
As an aside, the "UNION ALL
and exclusion JOIN
" method described on this page is a more reliable way of implementing FULL OUTER JOIN
in MySQL.
精彩评论