聯(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);