mysql 聯合查詢

2021-10-03 20:37:34 字數 625 閱讀 8349

高階9:聯合查詢

關鍵字:union:聯合、合併,將多條查詢語句的結果合併成乙個結果

intersect:交操作

except:差操作

引入:案例:查詢部門編號》90或者郵箱中包含a的員工資訊

select * from employees where department_id>90

union

select * from employees where email like '%a%';

應用場景

資訊來自多表,表之間沒有聯絡,但查詢的資訊一致時,使用聯合查詢

案例:查詢中國使用者男性的資訊+外國使用者男性的資訊

select id,cname,c*** from t_ca where c***='男'

union

select t_id,tname,tgader from t_ua  where tgader='male';

特點:    ①多條查詢語句的列數應該一致

②查詢語句之間每列的查詢型別和順序應該一致

③union關鍵字預設去重

④當查詢結果之間存在重複的時候,如果不想去重,可在union後新增all關鍵字。

mysql聯合查詢

有乙個前提很重要 就是兩個表中的對應字段應該是建立聯合關係且該鍵應唯一 在查詢該聯合建的時候要指明 表.欄位 1.select from 表a,表a子表 where表a.filecode 表a子表.filecodeand表a.id in select 表a子表 id from 表a子表 where ...

MySQL聯合查詢

1.select test.name,test2.name2 from test left join test2 on test.id test2.id 2.select test.name,test2.name2 from test right join test2 on test.id test...

mysql聯合查詢

mysql聯合查詢效率較高,以下例子來說明聯合查詢 內聯 左聯 右聯 全聯 的好處 t1表結構 使用者名稱,密碼 userid int usernamevarchar 20 passwordvarchar 20 1 jack jackpwd 2 owen owenpwd t2表結構 使用者名稱,積分...