說(shuō)明
▍ 子查詢(xún):嵌套在其他查詢(xún)中的查詢(xún)翘簇;
▍ 組合查詢(xún)(復(fù)合查詢(xún) / 并操作):執(zhí)行多次查詢(xún)撬码,多條select語(yǔ)句,并將結(jié)果作為單個(gè)查詢(xún)結(jié)果返回版保;
- union all:包含重復(fù)行;
- union:取消重復(fù)行;
▍ 聯(lián)結(jié)查詢(xún)
- inner join 內(nèi)聯(lián)結(jié)
保留兩邊表都有的記錄呜笑; - left join 左聯(lián)結(jié)
保留左邊表的信息,右邊表沒(méi)有匹配上的字段顯示為null找筝; - right join 右聯(lián)結(jié)
保留右邊表的信息蹈垢,左邊表沒(méi)有匹配上的字段顯示為null慷吊; - full join 全聯(lián)結(jié):
左右兩邊表的信息都保留袖裕,沒(méi)有匹配上的字段顯示為null;
練習(xí)
--1溉瓶、查詢(xún)來(lái)自安徽省的學(xué)生都在哪些班級(jí)(用子查詢(xún)做)急鳄;字段:學(xué)號(hào),姓名堰酿,省份疾宏,班級(jí);
select
distinct
stu_id as 學(xué)號(hào)
,name as 姓名
,from_where as 省份
,(select class from class_info where student_info.stu_id=class_info.stu_id ) as 班級(jí)
from student_info
where from_where='安徽省';
--方法二
select
distinct
stu_id as 學(xué)號(hào)
,name as 姓名
,'安徽省'
,class
from class_info
where stu_id in
(
--查詢(xún)來(lái)自安徽省的學(xué)生ID
select
distinct stu_id
from student_info
where from_where='安徽省'
);
--2触创、查詢(xún)每個(gè)班級(jí)考試不及格的學(xué)生信息(用子查詢(xún)做)坎藐;字段:班級(jí),學(xué)號(hào),姓名岩馍,學(xué)科碉咆,不及格分?jǐn)?shù);
select
(select class from class_info where score_info.stu_id=class_info.stu_id ) as 班級(jí)
,stu_id as 學(xué)號(hào)
,name as 姓名
,subject as 學(xué)科
,score as 不及格分?jǐn)?shù)
from score_info
where score<60
order by 班級(jí);
--3蛀恩、查詢(xún)每個(gè)班級(jí)考試不及格的學(xué)生人數(shù)(子查詢(xún)+聚合函數(shù))疫铜;字段:專(zhuān)業(yè),班級(jí)双谆,不及格人數(shù)壳咕;
select
distinct
major
,class
,(select count(if(score<60,stu_id,NULL)) from score_info where score_info.stu_id=class_info.stu_id) as 不及格人數(shù)
from class_info
group by major,class
order by major,class;
--方法2
major
,class
,count(distinct stu_id)
from class_info
where stu_id in
(
select
distinct stu_id
from score_info
where score<60
)
group by 1,2;
--4、查詢(xún) 電氣工程專(zhuān)業(yè) 和 糧食專(zhuān)業(yè) 的學(xué)生信息:姓名顽馋;
--簡(jiǎn)單查詢(xún)的方式;
select name
from student_info
where major in('電氣工程專(zhuān)業(yè)','糧食專(zhuān)業(yè)');
--組合查詢(xún)的方式:去重谓厘、不去重的區(qū)別;
--union all:包含重復(fù)行;
--union:取消重復(fù)行;
f1.name
from
(
select
name
from student_info
where major='糧食專(zhuān)業(yè)'
union all
select
name
from student_info
where major='電氣工程專(zhuān)業(yè)'
)f1;
--5、查詢(xún)物理和政治考試的學(xué)生信息(用組合查詢(xún)做)寸谜;字段:科目庞呕,學(xué)號(hào),姓名程帕,分?jǐn)?shù)住练;
select
f1.subject
,f1.stu_id
,f1.name
,f1.score
from
(
select
distinct
subject
,stu_id
,name
,score
from score_info
where subject='物理'
union all
select
subject
,stu_id
,name
,score
from score_info
where subject='政治'
)f1;
--6、查詢(xún)物理和政治考試的學(xué)生人數(shù)(組合查詢(xún)+聚合函數(shù))愁拭;字段:科目讲逛,考試人數(shù);
select
f1.*
from
(
select
subject
,count(distinct stu_id)
from score_info
where subject='政治'
group by 1
union all
select
subject
,count(distinct stu_id)
from score_info
where subject='物理'
group by 1
)f1;
--第二部分:聯(lián)結(jié)查詢(xún)
--1岭埠、查詢(xún)來(lái)自安徽省的學(xué)生都在哪些班級(jí)盏混;字段:學(xué)號(hào),姓名惜论,省份许赃,班級(jí);
select
distinct
f1.stu_id
,f1.name
,f1.from_where
,f2.class
from
(
select
distinct
stu_id
,name
,from_where
from student_info
where from_where='安徽省'
)f1
inner join
(
select
distinct
stu_id
,class
,name
from class_info
)f2 on f1.stu_id=f2.stu_id;
--2馆类、查詢(xún)每個(gè)班級(jí)沒(méi)有參加考試的學(xué)生信息混聊;字段:班級(jí),學(xué)號(hào)乾巧,姓名句喜;
select
distinct
f1.class
,f1.stu_id
,f1.name
from
(
--全部學(xué)生信息
select
distinct
stu_id
,name
,class
from class_info
)f1
left join
(
--參加了考試的學(xué)生id
select
distinct
stu_id
from student_info
)f2 on f1.stu_id=f2.stu_id
--篩選null
where f2.stu_id is null;
--3、查詢(xún)每個(gè)班級(jí)考試的平均分(聯(lián)結(jié)+聚合函數(shù))沟于;字段:學(xué)院咳胃,專(zhuān)業(yè),班級(jí)旷太,平均分展懈;
select
f1.college
,f1.major
,f1.class
,avg(f2.score)
from
(
select
distinct
college
,major
,class
,stu_id
from class_info
)f1
inner join
(
select
distinct
stu_id
,score
from score_info
)f2 on f1.stu_id=f2.stu_id
group by f1.college,f1.major,f1.class
order by f1.college,f1.major,f1.class;
--4、查詢(xún)每個(gè)班級(jí)考試不及格的人數(shù)(聯(lián)結(jié)+聚合函數(shù));字段:班級(jí)存崖,不及格人數(shù)榄攀;
select
f1.class as 班級(jí)
,count(f2.stu_id) as 不及格人數(shù)
from
(
select
distinct
stu_id
,class
from class_info
)f1
inner join
(
select
distinct
stu_id
from score_info
where score<60
)f2 on f1.stu_id=f2.stu_id
group by f1.class
order by f1.class;
--5、查詢(xún)每個(gè)班級(jí)金句,不同省份的人數(shù)(聯(lián)結(jié)+聚合函數(shù))檩赢;字段:班級(jí),省份违寞,人數(shù)贞瞒;
select
f1.class as 班級(jí)
,f2.from_where as 省份
,count(f1.stu_id) as 人數(shù)
from
(
select
distinct
class
,stu_id
from class_info
)f1
inner join
(
select
distinct
from_where
,stu_id
from student_info
)f2 on f1.stu_id=f2.stu_id
group by f1.class,f2.from_where
order by f1.class,f2.from_where;