sql语句的 union/union all
1. union 和 union all 的区别
- union 用于两张表联合后没有重复项
- union all 用于有重复项的表
2. 用法
- 用法:
- union: select user_id from uauth_userinfo union select price from shop;
- union all: select user_id from uauth_userinfo union all select user_id from uauth_unreadcount;
- 把union all联合后的数据当作一般的select后的数据进行处理的方法:
- 把整个数据用小括号()扩起来,后面加上 as name2 不写as也可以
- select user_id,count() from (select user_id from uauth_userinfo union all select user_id from uauth_unreadcount) as a group by user_id order by count();