2019-07-14

聯(lián)合查詢

基本語法:
select 語句1
union [union 選項(xiàng)]
select 語句2……

union選項(xiàng):()
all:保留所有,不管重復(fù)
distinct:去重,默認(rèn)

--同一張表
--聯(lián)合查詢多張表查詢 去重
select * from my_class 
union
select * from my_class;
--不去重
select * from my_class 
union all --不去重
select * from my_class;

--不相同的表查詢的內(nèi)容也不相同
select id,c_name,room from my_class 
union all --不去重
select name,number,id from my_student;

下面我們做一個(gè)小練習(xí)

--男生升序,女生降序
(select * from my_student where sex='男' order by age asc limit 99999999)
union
(select * from my_student where sex='女' order by age desc limit 99999999)

按位置分類

from子查詢

where子查詢

exists 子查詢

按結(jié)果分類

標(biāo)量子查詢

--一行一列
select * from my_student where c_id =(select id from my_class where c_name="python1809");

l列子查詢

select * from my_student where c_id 
in--多個(gè)數(shù)據(jù)源
(select id from my_class);

行子查詢

select * from my_student where age =(elect max(age)  from my_student)
ang
height =(select max(age) ,max(heiht) from my_student);


select * from my_student where (age,height) = (selet max (max),selet max (height)
from my_student);

select * from my_student  order by age besc,height desc limit 1;

表子查詢

select * from my_student where
exists (select * from my_class where id=1);



select * from my_student where
exists (select * from my_class where id=2);
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容