sql语句的 union/union all

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();
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章