-- 一定要设置别名,否则会导致结果不准确.select a.id aid,a.name aname,b.id bid,b.name bname from test_a a leftjoin test_b b on a.id=b.id
unionselect a.id aid,a.name aname,b.id bid,b.name bname from test_a a rightjoin test_b b on a.id=b.id;
union all
union all会对多个结果集进行并集操作,包括重复行,不进行排序.
如果返回的两个结果集中有重复的数据,那么返回的结果集就会包含重复的数据了.
另外,在生产过程中遇到这样的问题:
对多个表进行select count(*) 操作并对结果进行union all的时候发现显示的结果并不是按照select语句排列的,而是发生了乱序,针对这种情况,可以加一列进行区分。
-- 结果每次都不一样selectcount(*)from t1
unionallselectcount(*)from t2
unionallselectcount(*)from t3
unionallselectcount(*)from t4;-- 改写成下面写法就不怕了select't1',count(*)from t1
unionallselect't2',count(*)from t2
unionallselect't3',count(*)from t3
unionallselect't4',count(*)from t4;