數(shù)據(jù)表介紹
1.學(xué)生表
Student(SId,Sname,Sage,Ssex)
--SId 學(xué)生編號(hào),Sname 學(xué)生姓名,Sage 出生年月,Ssex 學(xué)生性別
2.課程表
Course(CId,Cname,TId)
--CId 課程編號(hào),Cname 課程名稱,TId 教師編號(hào)
3.教師表
Teacher(TId,Tname)
--TId 教師編號(hào),Tname 教師姓名
4.成績(jī)表
SC(SId,CId,score)
--SId 學(xué)生編號(hào),CId 課程編號(hào),score 分?jǐn)?shù)
學(xué)生表 Student
create table Student(SId varchar(10),Sname varchar(10),Sage datetime,Ssex varchar(10));
insert into Student values('01' , '趙雷' , '1990-01-01' , '男');
insert into Student values('02' , '錢(qián)電' , '1990-12-21' , '男');
insert into Student values('03' , '孫風(fēng)' , '1990-12-20' , '男');
insert into Student values('04' , '李云' , '1990-12-06' , '男');
insert into Student values('05' , '周梅' , '1991-12-01' , '女');
insert into Student values('06' , '吳蘭' , '1992-01-01' , '女');
insert into Student values('07' , '鄭竹' , '1989-01-01' , '女');
insert into Student values('09' , '張三' , '2017-12-20' , '女');
insert into Student values('10' , '李四' , '2017-12-25' , '女');
insert into Student values('11' , '李四' , '2012-06-06' , '女');
insert into Student values('12' , '趙六' , '2013-06-13' , '女');
insert into Student values('13' , '孫七' , '2014-06-01' , '女');
科目表 Course
create table Course(CId varchar(10),Cname nvarchar(10),TId varchar(10));
insert into Course values('01' , '語(yǔ)文' , '02');
insert into Course values('02' , '數(shù)學(xué)' , '01');
insert into Course values('03' , '英語(yǔ)' , '03');
教師表 Teacher
create table Teacher(TId varchar(10),Tname varchar(10));
insert into Teacher values('01' , '張三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');
成績(jī)表 SC
create table SC(SId varchar(10),CId varchar(10),score decimal(18,1));
insert into SC values('01' , '01' , 80);
insert into SC values('01' , '02' , 90);
insert into SC values('01' , '03' , 99);
insert into SC values('02' , '01' , 70);
insert into SC values('02' , '02' , 60);
insert into SC values('02' , '03' , 80);
insert into SC values('03' , '01' , 80);
insert into SC values('03' , '02' , 80);
insert into SC values('03' , '03' , 80);
insert into SC values('04' , '01' , 50);
insert into SC values('04' , '02' , 30);
insert into SC values('04' , '03' , 20);
insert into SC values('05' , '01' , 76);
insert into SC values('05' , '02' , 87);
insert into SC values('06' , '01' , 31);
insert into SC values('06' , '03' , 34);
insert into SC values('07' , '02' , 89);
insert into SC values('07' , '03' , 98);
以上是數(shù)據(jù)表介紹,但是我們MySQL肯定是沒(méi)有的,所以在開(kāi)始練習(xí)題目之前肯定是要先創(chuàng)建的:
先創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)
CREATE DATABASE student_db;
然后切換到這個(gè)數(shù)據(jù)庫(kù)
USE student_db;
然后再創(chuàng)建數(shù)據(jù)表,就是上面的create table,完成之后我們可以用SHOW TABLES;來(lái)查看所有表,效果如圖:
練習(xí)題目
- 查詢" 01 "課程比" 02 "課程成績(jī)高的學(xué)生的信息及課程分?jǐn)?shù)
select * from Student RIGHT JOIN (
select t1.SId, class1, class2 from
(select SId, score as class1 from sc where sc.CId = '01')as t1 查詢01課程成績(jī)并命名為class1
(select SId, score as class2 from sc where sc.CId = '02')as t2 查詢02課程成績(jī)并命名為class2
where t1.SId = t2.SId AND t1.class1 > t2.class2 關(guān)聯(lián)學(xué)號(hào)并篩選01課程成績(jī)大于02
)r
on Student.SId = r.SId; 關(guān)聯(lián)學(xué)生表獲取完整信息
select * from (
select t1.SId, class1, class2
from
(SELECT SId, score as class1 FROM sc WHERE sc.CId = '01') AS t1 取01課程成績(jī)
(SELECT SId, score as class2 FROM sc WHERE sc.CId = '02') AS t2 取02課程成績(jī)
where t1.SId = t2.SId and t1.class1 > t2.class2 篩選01成績(jī)大于02
) r
LEFT JOIN Student
ON Student.SId = r.SId; 左連接學(xué)生表查詢結(jié)果
結(jié)果:
1.1 查詢同時(shí)存在" 01 "課程和" 02 "課程的情況
select * from
(select * from sc where sc.CId = '01') as t1 查詢01課程所有記錄
,(select * from sc where sc.CId = '02') as t2 查詢02課程所有記錄
where t1.SId = t2.SId; 關(guān)聯(lián)學(xué)號(hào),查詢同時(shí)選01和02課程的學(xué)生
結(jié)果:
1.2 查詢存在" 01 "課程但可能不存在" 02 "課程的情況(不存在時(shí)顯示為 null )
select * from
(select * from sc where sc.CId = '01') as t1 篩選01號(hào)課程數(shù)據(jù)
left join
(select * from sc where sc.CId = '02') as t2 篩選02號(hào)課程數(shù)據(jù)
on t1.SId = t2.SId; 按學(xué)號(hào)左連接,保留01課程全部記錄
結(jié)果:
1.3 查詢不存在" 01 "課程但存在" 02 "課程的情況
select * from sc
where sc.SId not in ( 排除選了01課程的學(xué)生
select SId from sc
where sc.CId = '01'
)
AND sc.CId= '02'; 只保留02課程記錄
結(jié)果:
- 查詢平均成績(jī)大于等于 60 分的同學(xué)的學(xué)生編號(hào)和學(xué)生姓名和平均成績(jī)
select student.SId,sname,ss from student,(
select SId, AVG(score) as ss from sc 按學(xué)生計(jì)算平均成績(jī)
GROUP BY SId 按學(xué)號(hào)分組
HAVING AVG(score)> 60 篩選平均分大于60
)r
where student.sid = r.sid; 關(guān)聯(lián)學(xué)生表獲取姓名
結(jié)果:
- 查詢?cè)?SC 表存在成績(jī)的學(xué)生信息
select DISTINCT student.* 查詢學(xué)生信息并去重
from student,sc 從學(xué)生表和成績(jī)表查
where student.SId=sc.SId; 學(xué)號(hào)相等說(shuō)明有成績(jī)

- 查詢所有同學(xué)的學(xué)生編號(hào)、學(xué)生姓名、選課總數(shù)、所有課程的總成績(jī)(沒(méi)成績(jī)的顯示為 null )
select student.sid, student.sname,r.coursenumber,r.scoresum
from student,
(select sc.sid, sum(sc.score) as scoresum, count(sc.cid) as coursenumber from sc 計(jì)算總分和選課數(shù)
group by sc.sid)r 按學(xué)生分組
where student.sid = r.sid; 關(guān)聯(lián)學(xué)生表

4.1 查有成績(jī)的學(xué)生信息
select * from student
where exists (select sc.sid from sc where student.sid = sc.sid); 存在成績(jī)就顯示學(xué)生

- 查詢「李」姓老師的數(shù)量
select count(*) from teacher 統(tǒng)計(jì)老師的數(shù)量
where tname like '李%'; 篩選出姓李的老師

- 查詢學(xué)過(guò)「張三」老師授課的同學(xué)的信息
select student.* from student,teacher,course,sc 多表聯(lián)合查詢學(xué)生信息
where
student.sid = sc.sid 學(xué)生表與成績(jī)表按學(xué)號(hào)關(guān)聯(lián)
and course.cid=sc.cid 課程表與成績(jī)表按課程號(hào)關(guān)聯(lián)
and course.tid = teacher.tid 課程表與教師表按教師號(hào)關(guān)聯(lián)
and tname = '張三'; 篩選教師姓名為張三的授課記錄
);

- 查詢沒(méi)有學(xué)全所有課程的同學(xué)的信息
select * from student 查詢未學(xué)全所有課程的學(xué)生信息
where student.sid not in ( 排除已學(xué)完全部課程的學(xué)生
select sc.sid from sc 從成績(jī)表按學(xué)生分組
group by sc.sid
having count(sc.cid)= (select count(cid) from course) 選課數(shù)等于總課程數(shù)
);

- 查詢至少有一門(mén)課與學(xué)號(hào)為" 01 "的同學(xué)所學(xué)相同的同學(xué)的信
select * from student 查詢學(xué)生信息
where student.sid in ( 學(xué)號(hào)在指定范圍內(nèi)
select sc.sid from sc 查詢選過(guò)課的學(xué)生學(xué)號(hào)
where sc.cid in( 課程號(hào)在01號(hào)學(xué)生所選課程范圍內(nèi)
select sc.cid from sc 查詢01號(hào)學(xué)生所選的全部課程號(hào)
where sc.sid = '01'
)
);

- 查詢和" 01 "號(hào)的同學(xué)學(xué)習(xí)的課程 完全相同的其他同學(xué)的信息
select distinct sid from sc 查詢不重復(fù)的學(xué)號(hào)
where cid in(select cid from sc where sid='001') 課程在001學(xué)生學(xué)過(guò)的課程里
and sid != '001'; 排除001學(xué)生自己

- 查詢沒(méi)學(xué)過(guò)"張三"老師講授的任一門(mén)課程的學(xué)生姓名
select * from student 查詢學(xué)生信息
where student.sid not in( 排除上過(guò)張三老師課的學(xué)生
select sc.sid from sc where sc.cid in( 查詢上過(guò)對(duì)應(yīng)課程的學(xué)生學(xué)號(hào)
select course.cid from course where course.tid in( 查詢張三老師所授課程號(hào)
select teacher.tid from teacher where tname = "張三" 查詢張三老師編號(hào)
)
)
);
);

- 查詢兩門(mén)及其以上不及格課程的同學(xué)的學(xué)號(hào),姓名及其平均成績(jī)
select student.SId, student.Sname,b.avg 查詢學(xué)號(hào)、姓名、平均成績(jī)
from student RIGHT JOIN 右連接學(xué)生表與成績(jī)子查詢
(select sid, AVG(score) as avg from sc 計(jì)算符合條件學(xué)生的平均分
where sid in ( 篩選有兩門(mén)及以上不及格的學(xué)生
select sid from sc
where score<60 篩選不及格成績(jī)
GROUP BY sid
HAVING count(score)>1) 統(tǒng)計(jì)不及格課程數(shù)大于1
GROUP BY sid) b on student.sid=b.sid; 按學(xué)號(hào)關(guān)聯(lián)匹配學(xué)生信息

- 檢索" 01 "課程分?jǐn)?shù)小于 60,按分?jǐn)?shù)降序排列的學(xué)生信息
select student.*, sc.score 查詢學(xué)生信息與課程分?jǐn)?shù)
from student, sc 關(guān)聯(lián)學(xué)生表與成績(jī)表
where student.sid = sc.sid 按學(xué)號(hào)匹配
and sc.score < 60 篩選分?jǐn)?shù)小于60
and cid = "01" 限定01號(hào)課程
ORDER BY sc.score DESC; 按分?jǐn)?shù)降序排列

- 按平均成績(jī)從高到低顯示所有學(xué)生的所有課程的成績(jī)以及平均成績(jī)
select * from sc 查詢成績(jī)表所有字段
left join ( 左連接平均成績(jī)子查詢
select sid,avg(score) as avscore from sc 計(jì)算每個(gè)學(xué)生平均分
group by sid 按學(xué)號(hào)分組
)r
on sc.sid = r.sid 按學(xué)號(hào)匹配
order by avscore desc; 按平均分降序排列

- 查詢各科成績(jī)最高分、最低分和平均分:
以如下形式顯示:課程 ID,課程 name,最高分,最低分,平均分,及格率,中等率,優(yōu)良率,優(yōu)秀率
及格為>=60,中等為:70-80,優(yōu)良為:80-90,優(yōu)秀為:>=90
要求輸出課程號(hào)和選修人數(shù),查詢結(jié)果按人數(shù)降序排列,若人數(shù)相同,按課程號(hào)升序排列
select
sc.CId, 查詢課程編號(hào)
max(sc.score)as 最高分, 計(jì)算最高分
min(sc.score)as 最低分, 計(jì)算最低分
AVG(sc.score)as 平均分, 計(jì)算平均分
count(*)as 選修人數(shù), 統(tǒng)計(jì)選修人數(shù)
sum(case when sc.score>=60 then 1 else 0 end )/count(*)as 及格率, 計(jì)算及格率
sum(case when sc.score>=70 and sc.score<80 then 1 else 0 end )/count(*)as 中等率, 計(jì)算中等率
sum(case when sc.score>=80 and sc.score<90 then 1 else 0 end )/count(*)as 優(yōu)良率, 計(jì)算優(yōu)良率
sum(case when sc.score>=90 then 1 else 0 end )/count(*)as 優(yōu)秀率 計(jì)算優(yōu)秀率
from sc
GROUP BY sc.CId 按課程編號(hào)分組
ORDER BY count(*)DESC, sc.CId ASC; 按人數(shù)降序、課程號(hào)升序排列

- 按各科成績(jī)進(jìn)行排序,并顯示排名, Score 重復(fù)時(shí)保留名次空缺
select a.cid, a.sid, a.score, count(b.score)+1 as rank 查詢課程、學(xué)號(hào)、分?jǐn)?shù)、排名
from sc as a
left join sc as b 成績(jī)表自連接
on a.score<b.score and a.cid = b.cid 同課程內(nèi)分?jǐn)?shù)對(duì)比
group by a.cid, a.sid,a.score 按課程、學(xué)號(hào)、分?jǐn)?shù)分組
order by a.cid, rank ASC; 按課程、排名升序排列

15.1 按各科成績(jī)進(jìn)行排序,并顯示排名, Score 重復(fù)時(shí)合并名次
select a.cid,a.sid,a.score,count(DISTINCT b.score)+1 as rank 查詢課程號(hào)、學(xué)號(hào)、分?jǐn)?shù)、連續(xù)排名
from sc a left join sc b 查詢成績(jī)表自連接
on a.cid=b.cid and a.score<=b.score 同課程內(nèi)分?jǐn)?shù)小于等于
group by a.cid,a.sid,a.score 按課程、學(xué)號(hào)、分?jǐn)?shù)分組
order by a.cid,rank asc; 按課程、排名升序

- 查詢學(xué)生的總成績(jī),并進(jìn)行排名,總分重復(fù)時(shí)保留名次空缺
set @crank=0; 定義排名變量初始值為0
select q.sid, total, @crank := @crank +1 as rank from( 查詢學(xué)號(hào)、總分、排名
select sc.sid, sum(sc.score) as total from sc 計(jì)算每個(gè)學(xué)生總成績(jī)
group by sc.sid 按學(xué)號(hào)分組
order by total desc)q; 按總成績(jī)降序排列

16.1 查詢學(xué)生的總成績(jī),并進(jìn)行排名,總分重復(fù)時(shí)不保留名次空缺
set @crank=0; set @last=0; 定義排名變量與上一名分?jǐn)?shù)
select sid,total,case when @last=total then @crank else @crank:=@crank+1 end rank, @last:=total
from(
select sid,sum(score) total from sc 計(jì)算學(xué)生總成績(jī)
group by sid 按學(xué)號(hào)分組
order by total desc) t; 按總分降序,分?jǐn)?shù)相同排名相同

- 統(tǒng)計(jì)各科成績(jī)各分?jǐn)?shù)段人數(shù):課程編號(hào),課程名稱,[100-85],[85-70],[70-60],[60-0] 及所占百分比
select course.cname, course.cid, 查詢課程名稱、課程編號(hào)
sum(case when sc.score<=100 and sc.score>85 then 1 else 0 end) as "[100-85]", 統(tǒng)計(jì)85-100分人數(shù)
sum(case when sc.score<=85 and sc.score>70 then 1 else 0 end) as "[85-70]", 統(tǒng)計(jì)70-85分人數(shù)
sum(case when sc.score<=70 and sc.score>60 then 1 else 0 end) as "[70-60]", 統(tǒng)計(jì)60-70分人數(shù)
sum(case when sc.score<=60 and sc.score>0 then 1 else 0 end) as "[60-0]" 統(tǒng)計(jì)0-60分人數(shù)
from sc left join course 成績(jī)表左連接課程表
on sc.cid = course.cid 按課程號(hào)匹配
group by sc.cid; 按課程編號(hào)分組

- 查詢各科成績(jī)前三名的記錄
select * from sc 查詢成績(jī)信息
where ( 篩選前三名
select count(*) from sc as a
where sc.cid = a.cid and sc.score<a.score
)< 3 分?jǐn)?shù)高于當(dāng)前記錄的數(shù)量小于3
order by cid asc, sc.score desc; 按課程升序、分?jǐn)?shù)降序排列

- 查詢每門(mén)課程被選修的學(xué)生數(shù)
select cid, count(sid) from sc 查詢課程編號(hào)、選修人數(shù)
group by cid; 按課程編號(hào)分組統(tǒng)計(jì)

- 查詢出只選修兩門(mén)課程的學(xué)生學(xué)號(hào)和姓名
select student.sid, student.sname from student 查詢學(xué)號(hào)、姓名
where student.sid in 學(xué)號(hào)在指定范圍內(nèi)
(select sc.sid from sc
group by sc.sid
having count(sc.cid)=2); 篩選只選兩門(mén)課的學(xué)生

- 查詢男生、女生人數(shù)
select ssex, count(*) from student 查詢性別、對(duì)應(yīng)人數(shù)
group by ssex; 按性別分組統(tǒng)計(jì)

- 查詢名字中含有「風(fēng)」字的學(xué)生信息
select * 查詢所有學(xué)生信息
from student
where student.Sname like '%風(fēng)%'; 篩選姓名含“風(fēng)”字的學(xué)生

- 查詢同名同性學(xué)生名單,并統(tǒng)計(jì)同名人數(shù)
select sname, count(*) from student 查詢姓名、同名人數(shù)
group by sname
having count(*)>1; 篩選出現(xiàn)次數(shù)大于1的姓名

- 查詢 1990 年出生的學(xué)生名單
select * 查詢學(xué)生信息
from student
where YEAR(student.Sage)=1990; 篩選1990年出生的學(xué)生

- 查詢每門(mén)課程的平均成績(jī),結(jié)果按平均成績(jī)降序排列,平均成績(jī)相同時(shí),按課程編號(hào)升序排列
select sc.cid, course.cname, AVG(SC.SCORE) as average from sc, course 查詢課程號(hào)、課程名、平均分
where sc.cid = course.cid 按課程號(hào)匹配
group by sc.cid 按課程號(hào)分組
order by average desc,cid asc; 按平均分降序、課程號(hào)升序排列

- 查詢平均成績(jī)大于等于 85 的所有學(xué)生的學(xué)號(hào)、姓名和平均成績(jī)
select student.sid, student.sname, AVG(sc.score) as aver from student, sc 查詢學(xué)號(hào)、姓名、平均分
where student.sid = sc.sid 按學(xué)號(hào)匹配
group by sc.sid 按學(xué)號(hào)分組
having aver > 85; 篩選平均分大于85的學(xué)生

- 查詢課程名稱為「數(shù)學(xué)」,且分?jǐn)?shù)低于 60 的學(xué)生姓名和分?jǐn)?shù)
select student.sname, sc.score from student, sc, course 查詢姓名、分?jǐn)?shù)
where student.sid = sc.sid 按學(xué)號(hào)匹配
and course.cid = sc.cid 按課程號(hào)匹配
and course.cname = "數(shù)學(xué)" 限定課程為數(shù)學(xué)
and sc.score < 60; 篩選分?jǐn)?shù)低于60

- 查詢所有學(xué)生的課程及分?jǐn)?shù)情況(存在學(xué)生沒(méi)成績(jī),沒(méi)選課的情況)
select student.sname, cid, score from student 查詢姓名、課程號(hào)、分?jǐn)?shù)
left join sc 左連接成績(jī)表
on student.sid = sc.sid; 按學(xué)號(hào)匹配

- 查詢?nèi)魏我婚T(mén)課程成績(jī)?cè)?70 分以上的姓名、課程名稱和分?jǐn)?shù)
select student.sname, course.cname,sc.score from student,course,sc 查詢姓名、課程名、分?jǐn)?shù)
where sc.score>70 篩選分?jǐn)?shù)大于70
and student.sid = sc.sid 按學(xué)號(hào)匹配
and sc.cid = course.cid; 按課程號(hào)匹配

- 查詢不及格的課程
select DISTINCT sc.CId 查詢不重復(fù)的課程編號(hào)
from sc
where sc.score <60; 篩選不及格成績(jī)

- 查詢課程編號(hào)為 01 且課程成績(jī)?cè)?80 分以上的學(xué)生的學(xué)號(hào)和姓名
select student.sid,student.sname 查詢學(xué)號(hào)、姓名
from student,sc
where cid="01" 限定01號(hào)課程
and score>=80 篩選分?jǐn)?shù)大于等于80
and student.sid = sc.sid; 按學(xué)號(hào)匹配

- 求每門(mén)課程的學(xué)生人數(shù)
select sc.CId,count(*) as 學(xué)生人數(shù) 查詢課程編號(hào)、學(xué)生人數(shù)
from sc
GROUP BY sc.CId; 按課程編號(hào)分組統(tǒng)計(jì)

- 成績(jī)不重復(fù),查詢選修「張三」老師所授課程的學(xué)生中,成績(jī)最高的學(xué)生信息及其成績(jī)
select student.*, sc.score, sc.cid from student, teacher, course,sc 查詢學(xué)生信息、分?jǐn)?shù)、課程號(hào)
where teacher.tid = course.tid 按教師號(hào)匹配
and sc.sid = student.sid 按學(xué)號(hào)匹配
and sc.cid = course.cid 按課程號(hào)匹配
and teacher.tname = "張三" 限定教師為張三
order by score desc 按分?jǐn)?shù)降序排列
limit 1; 取最高分一條

- 成績(jī)有重復(fù)的情況下,查詢選修「張三」老師所授課程的學(xué)生中,成績(jī)最高的學(xué)生信息及其成績(jī)
select student.*, sc.score, sc.cid from student, teacher, course,sc 查詢學(xué)生信息、分?jǐn)?shù)、課程號(hào)
where teacher.tid = course.tid 按教師號(hào)匹配
and sc.sid = student.sid 按學(xué)號(hào)匹配
and sc.cid = course.cid 按課程號(hào)匹配
and teacher.tname = "張三" 限定教師為張三
and sc.score = ( 匹配最高分
select Max(sc.score)
from sc,student, teacher, course
where teacher.tid = course.tid
and sc.sid = student.sid
and sc.cid = course.cid
and teacher.tname = "張三");

- 查詢不同課程成績(jī)相同的學(xué)生的學(xué)生編號(hào)、課程編號(hào)、學(xué)生成績(jī)
select a.cid, a.sid, a.score from sc as a 查詢課程號(hào)、學(xué)號(hào)、分?jǐn)?shù)
inner join sc as b 內(nèi)連接成績(jī)表
on a.sid = b.sid 按學(xué)號(hào)匹配
and a.cid != b.cid 課程號(hào)不同
and a.score = b.score 分?jǐn)?shù)相同
group by cid, sid; 按課程號(hào)、學(xué)號(hào)分組去重

- 查詢每門(mén)功成績(jī)最好的前兩名
select a.sid,a.cid,a.score from sc as a 查詢學(xué)號(hào)、課程號(hào)、分?jǐn)?shù)
left join sc as b 自連接成績(jī)表
on a.cid = b.cid and a.score<b.score 同課程分?jǐn)?shù)對(duì)比
group by a.cid, a.sid 按課程號(hào)、學(xué)號(hào)分組
having count(b.cid)<2 篩選前兩名
order by a.cid; 按課程號(hào)排序

- 統(tǒng)計(jì)每門(mén)課程的學(xué)生選修人數(shù)(超過(guò) 5 人的課程才統(tǒng)計(jì))。
select sc.cid, count(sid) as cc from sc 查詢課程號(hào)、選修人數(shù)
group by cid 按課程號(hào)分組
having cc >5; 篩選人數(shù)超過(guò)5人的課程

- 檢索至少選修兩門(mén)課程的學(xué)生學(xué)號(hào)
select sid, count(cid) as cc from sc 查詢學(xué)號(hào)、選課數(shù)
group by sid 按學(xué)號(hào)分組
having cc>=2; 篩選至少選兩門(mén)課的學(xué)生

- 查詢選修了全部課程的學(xué)生信息
select student.* 查詢學(xué)生信息
from sc ,student
where sc.SId=student.SId 按學(xué)號(hào)匹配
GROUP BY sc.SId 按學(xué)號(hào)分組
HAVING count(*) = (select DISTINCT count(*) from course ); 選課數(shù)等于總課程數(shù)

- 查詢各學(xué)生的年齡,只按年份來(lái)算
SELECT
SId 學(xué)生學(xué)號(hào)
Sname 學(xué)生姓名
YEAR(CURDATE()) - YEAR(Sage) AS age 按年份差計(jì)算年齡
FROM Student;

- 按照出生日期來(lái)算,當(dāng)前月日 < 出生年月的月日則,年齡減一
select student.SId as 學(xué)生編號(hào),student.Sname as 學(xué)生姓名, 查詢學(xué)號(hào)、姓名
TIMESTAMPDIFF(YEAR,student.Sage,CURDATE()) as 學(xué)生年齡 精確計(jì)算年齡
from student;

- 查詢本周過(guò)生日的學(xué)生
select * 查詢學(xué)生信息
from student
where WEEKOFYEAR(student.Sage)=WEEKOFYEAR(CURDATE()); 篩選本周過(guò)生日的學(xué)生
生日月份只有 1 月、6 月、12 月,沒(méi)有 4 月的生日,所以查詢會(huì)返回空集
- 查詢下周過(guò)生日的學(xué)生
select * 查詢學(xué)生信息
from student
where WEEKOFYEAR(student.Sage)=WEEKOFYEAR(CURDATE())+1; 篩選下周過(guò)生日的學(xué)生
生日月份只有 1 月、6 月、12 月,沒(méi)有 4 月的生日,所以查詢會(huì)返回空集
- 查詢本月過(guò)生日的學(xué)生
select * 查詢學(xué)生信息
from student
where MONTH(student.Sage)=MONTH(CURDATE()); 篩選本月過(guò)生日的學(xué)生
生日月份只有 1 月、6 月、12 月,沒(méi)有 4 月的生日,所以查詢會(huì)返回空集
- 查詢下月過(guò)生日的學(xué)生
select * 查詢學(xué)生信息
from student
where MONTH(student.Sage)=MONTH(CURDATE())+1; 篩選下月過(guò)生日的學(xué)生
生日月份只有 1 月、6 月、12 月,沒(méi)有 45月的生日,所以查詢會(huì)返回空集
小結(jié)
通過(guò)這次數(shù)據(jù)庫(kù)練習(xí),我熟練掌握了 SQL 語(yǔ)句的使用方法,提升了邏輯思維與問(wèn)題解決能力,也增強(qiáng)了細(xì)心排查錯(cuò)誤的習(xí)慣,整體專業(yè)實(shí)操能力得到了明顯鍛煉。