开发者

how do i select the required attributes from a union of left join and right join

开发者 https://www.devze.com 2023-02-28 23:26 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号