mysql聯合查詢怎麼去重 MySql 聯合查詢

2021-10-19 17:09:15 字數 678 閱讀 4985

#高階9:聯合查詢

union 聯合 合併:將多條查詢語句的結果合併成乙個結果

語法:查詢語句1

union

查詢語句2

union

應用場景:

要查詢的結果來自於多個表,且多個表沒有直接的連線關係,但查詢的資訊一致時

特點:★

1、要求多條查詢語句的查詢列數是一致的!

2、要求多條查詢語句的查詢的每一列的型別和順序最好一致

3、union關鍵字預設去重,如果使用union all 可以包含重複項

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

select * from employees where email like '%a%' or department_id>90;;

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

union

select * from employees where department_id>90;

#案例:查詢中國使用者中男性的資訊以及外國使用者中年男性的使用者資訊

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

union all

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

mysql 查詢 去重

insert into material id file url select id,ekm.file url from extmaterial as ekm left join select file url from material where id id as pm on pm.file u...

mysql搜尋結果去重 MySQL去除查詢結果重複

出現結果重複數sql 四表關聯 select count post.id from wp posts as post left join wp term relationships as relation on post.menu order relation.term order left joi...

yii2 mysql 去重 yii2 查詢去重

mysql 查詢去重 distinct 待完善內容.1.實現查詢去重.分頁,例如 實現依據qid去重,createtime排序,命令行為 get nb luban answer search sort aggs 1.distinct select count distinct cname from ...