sql中union,union all没有兼顾到的内容

今日遇到一个问题,两张表联合取交集去重,但是需要把某一字段相同的也给去掉

union all : 联合,没有取交集

union :联合取交集(仅针对所有字段相同的去重)

解决方案:将联合的数据作为一个临时表,然后group by,再对所选字段取max就可以达到想要的结果了。

下面省略了union联表

1 select max(l.phone),
2        max(l.login_name),
3        max(l.zone),
4        max(l.nick_name),
5        max(l.spa_id)
6   from lostuser l
7  group by l.phone

猜你喜欢

转载自www.cnblogs.com/pupilheart/p/11134111.html