一:學(xué)生卖毁、課程揖曾、分?jǐn)?shù)、教師表建立
student學(xué)生表
create table Student(
Sno varchar(10),
Sname varchar(10),
Sage datetime,
Ssex varchar(10));
insert into Student values('01' , '趙雷' , '1990-01-01' , '男');
insert into Student values('02' , '錢電' , '1990-12-21' , '男');
insert into Student values('03' , '孫風(fēng)' , '1990-05-20' , '男');
insert into Student values('04' , '李云' , '1990-08-06' , '男');
insert into Student values('05' , '周梅' , '1991-12-01' , '女');
insert into Student values('06' , '吳蘭' , '1992-03-01' , '女');
insert into Student values('07' , '鄭竹' , '1989-07-01' , '女');
insert into Student values('08' , '王菊' , '1990-01-20' , '女');
course課程表
create table Course(
Cno varchar(10),
Cname nvarchar(10),
T varchar(10));
insert into Course values('01' , '語文' , '02');
insert into Course values('02' , '數(shù)學(xué)' , '01');
insert into Course values('03' , '英語' , '03');
teacher教師表
create table Teacher(
Tno varchar(10),
Tname nvarchar(10));
insert into Teacher values('01' , '張三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');
sco成績表
create table SCo(
Sno varchar(10),
Cno varchar(10),
score decimal(18,1));
insert into SCo values('01' , '01' , 80);
insert into SCo values('01' , '02' , 90);
insert into SCo values('01' , '03' , 99);
insert into SCo values('02' , '01' , 70);
insert into SCo values('02' , '02' , 60);
insert into SCo values('02' , '03' , 80);
insert into SCO values('03' , '01' , 80);
insert into SCo values('03' , '02' , 80);
insert into SCo values('03' , '03' , 80);
insert into SCo values('04' , '01' , 50);
insert into SCo values('04' , '02' , 30);
insert into SCo values('04' , '03' , 20);
insert into SCo values('05' , '01' , 76);
insert into SCo values('05' , '02' , 87);
insert into SCo values('06' , '01' , 31);
insert into SCo values('06' , '03' , 34);
insert into SCo values('07' , '02' , 89);
insert into SCo values('07' , '03' , 98);
二:練習(xí)題
1.1 查詢同時存在" 01 "課程和" 02 "課程的情況
方法一:
select
*
from (select * from sco where cno="01")a
inner join (select * from sco where cno="02") b
on a.sno=b.sno;
方法二:
select
*
from sco a
inner join sco b
on a.sno=b.sno
where a.cno='01' and b.cno='02';
1.2 查詢存在" 01 "課程但可能不存在" 02 "課程的情況(不存在時顯示為 null )
select
*
from (SELECT * FROM SCO WHERE CNO='01') a
left join (SELECT * FROM SCO WHERE CNO='02') b
on a.sno=b.sno;
方法二:
select
*
from (SELECT * FROM SCO WHERE CNO='01') a
left join sco b
on a.sno=b.sno and b.cno='02';
1.3 查詢不存在" 01 "課程但存在" 02 "課程的情況
select
*
from (select * from sco where cno='02') a
where a.sno not in (select sno from sco where cno='01');
- 查詢平均成績大于等于 60 分的同學(xué)的學(xué)生編號和學(xué)生姓名和平均成績
select * from sco;
select
a.sno,
a.sname,
b.avg_score
from student a
inner join
(select
sno,
avg(score) as avg_score
from sco
group by sno having avg_score>=60) b
on a.sno=b.sno;`
- 查詢在 SC 表存在成績的學(xué)生信息
select
s.*
from student s
inner join
(select
distinct sno
from sco where score is not null) b
on s.sno=b.sno;
- 查詢所有同學(xué)的學(xué)生編號亥啦、學(xué)生姓名炭剪、選課總數(shù)、所有課程的總成績(沒成績的顯示為 null )
select
a.sno,
a.sname,
b.cons,
b.sum_score
from student a
left join
(select
sno,
count(1) as cons,
sum(score) as sum_score
from sco
group by sno) b
on a.sno=b.sno;
這個執(zhí)行不了的原因翔脱?奴拦??
select
a.sno,
a.sname,
count(b.cno) as cons,
sum(b.score) as sum_score
from student a
left join sco b
on a.sno=b.sno group by a.sno;
- 查詢「李」姓老師的數(shù)量
select
count(*) as cons
from teacher
where tname like '李%';
#6. 查詢學(xué)過「張三」老師授課的同學(xué)的信息
select * from course;
select * from student;
select * from sco;
select * from teacher;
先求出張三老師教授的課程届吁,關(guān)聯(lián) course 和teacher表;
select
c.cno
from teacher t
join course c
on t.tno=c.t
where t.tname='張三';
在求出對應(yīng)此課程學(xué)習(xí)的學(xué)生編號错妖;
select
sno
from sco
where cno= (select c.cno from teacher t join course c on t.tno=c.t where t.tname='張三');
關(guān)聯(lián)張三
select
a.*
from student a
inner join (select sno
from sco
where cno= (select c.cno from teacher t join course c on t.tno=c.t where t.tname='張三')) b on a.sno=b.sno;
- 查詢沒有學(xué)全所有課程的同學(xué)的信息 (是否包含8王菊) 如包含left 如不包含不加left;
select
a.*
from student a
join sco b
on a.sno=b.sno
group by a.sno
having count(b.cno)<(select count(cno) from course);
- 查詢至少有一門課與學(xué)號為" 01 "的同學(xué)所學(xué)相同的同學(xué)的信息
select cno from sco where sno=01;
select
sno
from sco
where cno in (select cno from sco where sno=01)
group by sno;
select
*
from student s
right join
(select
sno
from sco
where cno in (select cno from sco where sno=01) group by sno) b
on s.sno=b.sno;
- 查詢和" 01 "號的同學(xué)學(xué)習(xí)的課程完全相同的其他同學(xué)的信息
select cno from sco where sno='01';
select sno from sco where cno not in (select cno from sco where sno='01');
select count(cno) as cnos from course;
select
a.*
from student a
inner join sco b
on a.sno=b.sno
where a.sno not in (select sno from sco where cno not in (select cno from sco where sno='01'))
group by a.sno
having count(b.cno)=(select count(cno) as cnos from sco where sno='01') and a.sno<>1;
- 查詢沒學(xué)過"張三"老師講授的任一門課程的學(xué)生姓名
select
a.sno
from sco a
left join course b
on a.cno=b.cno
left join teacher c
on c.tno=b.t
where c.Tname='張三';
select
*
from student
where sno not (select
a.sno
from sco a
left join course b
on a.cno=b.cno
left join teacher c
on c.tno=b.t
where c.Tname='張三');
???#11. 查詢兩門及其以上不及格課程的同學(xué)的學(xué)號绿鸣,姓名及其平均成績
select
a.sno,
b.sname,
avg(a.score)
from (select * from sco where score<60) a
left join student b
on a.sno=b.sno
group by a.sno
having count(a.sno)>=2;
- 檢索" 01 "課程分?jǐn)?shù)小于 60,按分?jǐn)?shù)降序排列的學(xué)生信息
select
sno,
score
from sco
where cno='01'and score<60;
select
b.*,
a.score
from (select
sno,
score
from sco
where cno='01'and score<60) a
left join student b
on a.sno=b.sno
order by a.score desc;
select
b.*,
a.score
from sco a
left join student b
on a.sno=b.sno
where a.score<60 and a.cno='01' order by a.score desc;
- 按平均成績從高到低顯示所有學(xué)生的所有課程的成績以及平均成績
select
sno,
avg(score) as avg_score
from sco
group by sno;
select
a.*,
b.avg_score
from sco a
left join (select
sno,
avg(score) as avg_score
from sco
group by sno) b
on a.sno=b.sno
order by b.avg_score desc;
- 查詢各科成績最高分暂氯、最低分和平均分:
以如下形式顯示:課程 ID潮模,課程 name,最高分痴施,最低分擎厢,平均分,及格率辣吃,中等率动遭,優(yōu)良率,優(yōu)秀率
及格為>=60神得,中等為:70-80厘惦,優(yōu)良為:80-90,優(yōu)秀為:>=90
要求輸出課程號和選修人數(shù)哩簿,查詢結(jié)果按人數(shù)降序排列绵估,若人數(shù)相同,按課程號升序排列
提示:分組聚合卡骂,這里考察條件計數(shù)技巧国裳;
1/各科的平均分/最高分/及格率;
select
cno,
max(score) as '最高分',
min(score) as '最低分',
avg(score) as '平均分',
count(1) as '選修人數(shù)',
sum(case when score>=60 then 1 else 0 end)/count(1) as '及格率',
sum(case when score >=70 and score<80 then 1 else 0 end)/count(1) as '中等率',
sum(case when score >=80 and score<90 then 1 else 0 end)/count(1) as '優(yōu)良率',
sum(case when score >=90 then 1 else 0 end)/count(1) as '優(yōu)秀率'
from sco
group by cno
order by '選修人數(shù)' desc, cno asc;
#15. 按各科成績進(jìn)行排序全跨,并顯示排名缝左, Score 重復(fù)時保留名次空缺
select @rank:=1;
select @rank;
select
a.*,
@rank:=@rank+1 as rank
from sco as a ,(select @rank:=0) as t
order by score desc;
15.1 按各科成績進(jìn)行排序,并顯示排名浓若, Score 重復(fù)時合并名次
select
sno,
cno,
score,
case when @sco=score then @rank
else @rank:=@rank+1 end as rn,
@sco:=score
from sco, (select @rank:=0, @score:=null) as t
order by score desc;
select
sno,
cno,
score,
case when @sco=score then @rank
when @sco:=score then @rank:=@rank+1
end as rn
from sco, (select @rank:=0, @sco:=null) as t
order by score desc;
#16. 查詢學(xué)生的總成績渺杉,并進(jìn)行排名,總分重復(fù)時保留名次空缺
#自定義變量
#定義方法一:
set @a:=2;
#定義方法二:
select @b:=4;
#重新賦值就是修改值挪钓;
select @b:=6;
#先求學(xué)生的總成績:
select
sno,
sum(score) as scos
from sco
group by sno
order by scos desc;
#名次排序:
select
a.*,
@rank:=if(@sco=scos,'',@rank+1) as rank,
@sco:=scos
from (select
sno,
sum(score) as scos
from sco
group by sno
order by scos desc) a
inner join
(select @sco:=null, @rank:=0) b;
16.1 查詢學(xué)生的總成績是越,并進(jìn)行排名,總分重復(fù)時不保留名次空缺
select
a.*,
@rank:=if(@sco=scos, @rank, @rank+1) as rank,
@sco:=scos
from (select
sno,
sum(score) as scos
from sco
group by sno
order by scos desc) a
inner join
(select @sco:=null, @rank:=0) b;
- 統(tǒng)計各科成績各分?jǐn)?shù)段人數(shù):課程編號碌上,課程名稱倚评,[100-85],[85-70]馏予,[70-60]天梧,[60-0] 及所占百分比
select* from course
select
a.cno,
b.Cname,
count(sno) as '選修人數(shù)',
sum(case when score>=85 then 1 else 0 end)/count(1) as '[100-85]',
sum(case when score>=70 and score<85 then 1 else 0 end)/count(1) as '[85-70]',
sum(case when score>=60 and score<70 then 1 else 0 end)/count(1) as '[70-60]',
sum(case when score>=0 and score<60 then 1 else 0 end)/count(1) as '[60-0]'
from sco a
left join course b
on a.cno=b.cno
group by cno;
#增加% select concat('date','frog')用法;
select
a.cno,
b.Cname,
count(sno) as '選修人數(shù)',
concat(sum(case when score>=85 then 1 else 0 end)/count(1) *100, '%') as '[100-85]',
concat(sum(case when score>=70 and score<85 then 1 else 0 end)/count(1) *100,'%') as '[85-70]',
concat(sum(case when score>=60 and score<70 then 1 else 0 end)/count(1) *100,'%') as '[70-60]',
concat(sum(case when score>=0 and score<60 then 1 else 0 end)/count(1) *100,'%') as '[60-0]'
from sco a
left join course b
on a.cno=b.cno
group by cno;
- 查詢各科成績前三名的記錄
select
a.*
from sco a
where (select count(*) from sco b where a.cno=b.cno and a.score<b.score)<3
order by cno desc, score desc;
- 查詢每門課程被選修的學(xué)生數(shù)
select
cno,
count(1) as cons
from sco
group by cno;
- 查詢出只選修兩門課程的學(xué)生學(xué)號和姓名
select
a.sno,
b.sname
from sco a
left join student b
on a.sno=b.sno
group by a.sno,b.sname
having count(1)=2;
- 查詢男生霞丧、女生人數(shù)
select
ssex,
count(ssex) as cons
from student
group by ssex;
- 查詢名字中含有「風(fēng)」字的學(xué)生信息
select
*
from student
where sname like '%風(fēng)%';
??#23. 查詢同名同性學(xué)生名單呢岗,并統(tǒng)計同名人數(shù)
select
a.sname,
a.ssex,
count(a.sno) as cons
from student a
inner join student b
on a.sname=b.sname and a.ssex=b.ssex and a.sno!=b.sno
group by a.sname, a.ssex;
- 查詢 1990 年出生的學(xué)生名單 year 函數(shù) select year('2020-02-01')
select
*
from student
where date_format(sage, '%Y')=1990;
select
*
from student
where year(sage)='1990';
25.查詢每門課程的平均成績,結(jié)果按平均成績降序排列,平均成績相同時后豫,按課程編號升序排列
select
cno,
avg(score) as avg_score
from sco
group by cno
order by avg_score desc, cno asc;
- 查詢平均成績大于等于 85 的所有學(xué)生的學(xué)號悉尾、姓名和平均成績
select
a.sno,
b.sname,
avg(a.score) as avg_score
from sco a
left join student b
on a.sno=b.sno
group by a.sno
having avg_score>=85;
- 查詢課程名稱為「數(shù)學(xué)」,且分?jǐn)?shù)低于 60 的學(xué)生姓名和分?jǐn)?shù)
方法一
select
c.sname,
b.cname,
a.score
from sco a
left join course b
on a.cno=b.cno
left join student c
on a.sno=c.sno
where b.cname='數(shù)學(xué)' and a.score<60;
#方法二
select
a.sname,
b.score
from student a
join sco b
on a.sno=b.sno
where b.cno=(select cno from course where cname='數(shù)學(xué)')
and b.score<60;
- 查詢所有學(xué)生的課程及分?jǐn)?shù)情況(存在學(xué)生沒成績挫酿,沒選課的情況)
select
a.*,
b.cno,
b.score,
c.cname,
c.t
from student a
left join sco b
on a.sno=b.sno
left join course c
on b.cno=c.cno;
29.查詢?nèi)魏我婚T課程成績在 70 分以上的姓名焕襟、課程名稱和分?jǐn)?shù)
select
b.sname,
c.cname,
a.score
from sco a
left join student b
on a.sno=b.sno
left join course c
on a.cno=c.cno
where a.score>70;
#30.查詢不及格的課程
select
a.*
from course a
where a.cno in (select distinct cno from sco where score<60);
- 查詢課程編號為 01 且課程成績在 80 分以上的學(xué)生的學(xué)號和姓名
select
a.sno,
b.sname
from sco a
left join student b
on a.sno=b.sno
where a.cno='01' and a.score>80;
32. 求每門課程的學(xué)生人數(shù)
select
cno,
count(1) as cons
from sco
group by cno;
- 成績不重復(fù),查詢選修「張三」老師所授課程的學(xué)生中饭豹,成績最高的學(xué)生信息及其成績
select
b.*,
max(a.score) as max_score
from sco a
left join student b
on a.sno=b.sno
left join course c
on a.cno=c.cno
left join teacher t
on t.tno=c.t
where t.tname='張三';
???#34. 成績有重復(fù)的情況下鸵赖,查詢選修「張三」老師所授課程的學(xué)生中,成績最高的學(xué)生信息及其成績
select
*
from
(select
a.*,
case when @score=score then @rank
when @score:=score then @rank:=@rank+1 end as rn
from
(select
b.*,
a.score
from sco a
left join student b
on a.sno=b.sno
left join course c
on a.cno=c.cno
left join teacher t
on t.tno=c.t
where t.tname='張三') a, (select @score:=null, @rank:=0) t) s;
???#35. 查詢不同課程成績相同的學(xué)生的學(xué)生編號拄衰、課程編號它褪、學(xué)生成績
select
a.*
from sco a
inner join sco b
on a.sno=b.sno
where a.cno!=b.cno and a.score=b.score
group by a.sno,a.cno;
???#36. 查詢每門功成績最好的前兩名 #用戶變量
select
sno,
cno,
score,
rank
from
(select
sco.*,
@rank:=if(@c_cno=cno, if(@sco=score, @rank, @rank+1), 1) as rank,
@sco:=score,
@c_cno:=cno
from sco, (select @sco:=null, @rank:=0, @c_cno:=null) b
order by cno, score desc) a
where a.rank<3;
- 統(tǒng)計每門課程的學(xué)生選修人數(shù)(超過 5 人的課程才統(tǒng)計)。
select
cno,
count(sno) as cons
from sco
group by cno
having count(1)>5;
- 檢索至少選修兩門課程的學(xué)生學(xué)號
select
sno,
count(1) as cons
from sco
group by sno
having count(1)>=2
- 查詢選修了全部課程的學(xué)生信息
select
*
from student
where sno
in (select sno from sco group by sno having count(sno)=(select count(distinct cno) from sco));
select
sno
from sco
group by sno
having count(1)=(select count(1) from course);
select
a.*
from student a
where (select count(1) from sco b where a.sno=b.sno)
=(select count(1) from course);
#最好是這么寫
select
*
from student a
right join
(select
sno
from sco
group by sno
having count(1)=(select count(*) from course)) b
on a.sno=b.sno;
- 查詢各學(xué)生的年齡翘悉,只按年份來算 year 函數(shù)
select CURRENT_DATE() #整取年月日
select CURDATE() #整取年月日
select now() #年月日時間
#方法一:
select
*,
(date_format(now(), '%Y')-date_format(sage, '%Y')) as age
from student;
#方法二:
select
*,
year(now())-year(sage) as age
from student;
- 按照出生日期來算茫打,當(dāng)前月日 < 出生年月的月日則,年齡減一
select* from student;
#timestampdiff 日期相減函數(shù)
select timestampdiff(year, '2002-05-01', '2003-06-01 00:10:50');
select timestampdiff(day, '2002-05-01', '2001-01-01');
select timestampdiff(hour, '2008-08-08 12:00:00', '2008-08-08 00:00:00');
#獲取當(dāng)前時間
select now();
select curdate();
select
*,
timestampdiff(year, sage, now()) as age
from student;
- 查詢本周過生日的學(xué)生 week 函數(shù)
select week('2020/5/21');
select
*,
week(sage),
week(now())
from student
where week(sage)=week(now());
- 查詢下周過生日的學(xué)生
select week(now())+1
select
*,
week(sage),
week(now())+1
from student
where week(sage)=week(now())+1
- 查詢本月過生日的學(xué)生
方法一:
select *
from student
where date_format(sage,'%m')=date_format(now(),'%m');
方法二:
select
*
from student
where month(sage)=month(now());
- 查詢下月過生日的學(xué)生;
select
*
from student
where month(sage)=month(now())+1;
- 查詢上月過生日的學(xué)生;
select *
from student
where date_format(sage,'%m')=date_format(date_sub(now(), interval 2 month),'%m');
select
*
from student
where month(sage)=month(now())-1;```