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