1.union 多个表的时候,每个表有group by的情况下,可以过滤重复的数据
2.union all 多个表的时候 ,每个表都有group by的情况下也不过滤重复数据
举个例子:
现在有两个表,user表根据passport字段group by 共有 a b c d 四条数据
user_b 表根据passport字段group by 共有 a b c d e 五条数据
union 去重集合,user表跟user_b根据passport字段去重
结果是 a b c d + a b c d e的集合去重就是 a b c d e
select count(*) from (
(select passport from `user` group by passport)
union
(select passport from `user_b` group by passport)
) as x
union all 并集,如果需要a+b的并集是:结果4 + 5 = 9
结果是 a b c d + a b c d e的集合不去重就是a b c d a b c d e = 9
select count(*) from (
(select passport from `user` group by passport)
union all
(select passport from `user_b` group by passport)
) as x