當前位置:首頁 > IT技術(shù) > 數(shù)據(jù)庫 > 正文

2021-8-5 Mysql個人練習題
2021-10-08 17:33:23

創(chuàng)建學校表格

CREATE TABLE `Student`(
`s_id` VARCHAR(20),
`s_name` VARCHAR(20) NOT NULL DEFAULT '',
`s_birth` VARCHAR(20) NOT NULL DEFAULT '',
`s_sex` VARCHAR(10) NOT NULL DEFAULT '',
PRIMARY KEY(`s_id`)
);

CREATE TABLE `Course`(
`c_id` VARCHAR(20),
`c_name` VARCHAR(20) NOT NULL DEFAULT '',
`t_id` VARCHAR(20) NOT NULL,
PRIMARY KEY(`c_id`)
);

CREATE TABLE `Teacher`(
`t_id` VARCHAR(20),
`t_name` VARCHAR(20) NOT NULL DEFAULT '',
PRIMARY KEY(`t_id`)
);

CREATE TABLE `Score`(
`s_id` VARCHAR(20),
`c_id` VARCHAR(20),
`s_score` INT(3),
PRIMARY KEY(`s_id`,`c_id`)
);

insert into Student values('01' , '趙雷' , '1990-01-01' , '');
insert into Student values('02' , '錢電' , '1990-12-21' , '');
insert into Student values('03' , '孫風' , '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' , '');

insert into Course values('01' , '語文' , '02');
insert into Course values('02' , '數(shù)學' , '01');
insert into Course values('03' , '英語' , '03');

insert into Teacher values('01' , '張三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');


insert into Score values('01' , '01' , 80);
insert into Score values('01' , '02' , 90);
insert into Score values('01' , '03' , 99);
insert into Score values('02' , '01' , 70);
insert into Score values('02' , '02' , 60);
insert into Score values('02' , '03' , 80);
insert into Score values('03' , '01' , 80);
insert into Score values('03' , '02' , 80);
insert into Score values('03' , '03' , 80);
insert into Score values('04' , '01' , 50);
insert into Score values('04' , '02' , 30);
insert into Score values('04' , '03' , 20);
insert into Score values('05' , '01' , 76);
insert into Score values('05' , '02' , 87);
insert into Score values('06' , '01' , 31);
insert into Score values('06' , '03' , 34);
insert into Score values('07' , '02' , 89);
insert into Score values('07' , '03' , 98)
View Code

?

SELECT s2.s_name,s2.s_birth,s2.s_sex,s1.s_score,s3.s_score from student s2
left join score s1 on s1.s_id=s2.s_id  and s1.c_id="02"
left join score s3 on s3.s_id=s2.s_id and s3.c_id="01"
where s3.s_score>s1.s_score;#查詢課程01比02的課程分數(shù)的同學信息

SELECT s1.s_id,(s1.s_score) avgScore,s2.s_name from score s1 
left join student s2 on s1.s_id=s2.s_id
group by s1.s_id HAVING avgScore>=60;#查詢平均成績大于60的同學信息


SELECT s2.s_id,s2.s_name,count(s1.c_id),sum(s1.s_score) from student s2
left join score s1 on s2.s_id=s1.s_id GROUP BY s_id;#查詢學生編號、學生姓名、課程數(shù)、課程總分

SELECT count(*) from teacher where t_name like '張%';#查詢姓張的老師數(shù)量

SELECT s2.* from student s2
left join score s1 on s2.s_id=s1.s_id
left join course c1 on c1.c_id=s1.c_id
left join teacher t1 on t1.t_id=c1.t_id
where t1.t_name like "張三";#查詢張三教的學生的所有信息

SELECT * from student where s_id not in(
SELECT s2.s_id from student s2
left join score s1 on s2.s_id=s1.s_id
left join course c1 on c1.c_id=s1.c_id
left join teacher t1 on t1.t_id=c1.t_id
where t1.t_name like "張三");#查詢不是張三教的學生的所有信息

?

select * from student  where s_id in (select s1.s_id from score s1 inner join score s2 on s1.s_id=s2.s_id where s1.c_id="01" and s2.c_id="02");
#查詢學過編號為"01"并且也學過編號為"02"的課程的同學的信息

select * from student  where s_id in (select s1.s_id from score s1 inner join score s2 on s1.s_id=s2.s_id where s1.c_id="01" and s2.c_id!="02");
#查詢學過編號為"01"但沒有學過編號為"02"的課程的同學的信息

SELECT * from student where s_id in(SELECT s1.s_id from score s1 GROUP BY s_id HAVING count(s1.c_id)>=(select count(DISTINCT(c_id)) from score));
#查詢所有課程都學過的同學信息

SELECT * from student where s_id in (SELECT s_id from score group by s_id HAVING GROUP_CONCAT(c_id)=(SELECT GROUP_CONCAT(c_id) from score s1 where s_id='01' group by s_id));
#查詢所有課程都與01同學相同的同學信息

select s_name from student where s_id in(SELECT s_id from score where c_id =(select c_id from course where t_id=(SELECT t_id from teacher where t_name="張三")));
#查詢學過張三課程的學生信息

SELECT s1.s_id,s1.s_name,avg(s2.s_score) from student s1
left join score s2 on s1.s_id=s2.s_id 
where s2.s_id in (SELECT s_id from score where (s_score<60 or isnull(s_score)) GROUP BY s_id HAVING count(s_id)>=2) group by s1.s_id,s1.s_name ;
#查詢兩門或兩門以上不及格分數(shù)的學生學號,姓名和平均分

SELECT s1.*,s2.s_score from student s1
left join score s2 on s1.s_id=s2.s_id
where s2.c_id='01' and s2.s_score<=60 ORDER BY s2.s_score desc;
#查詢01課程成績小于60分并且按分數(shù)降序排列



SELECT s1.s_name,s2.s_score,s3.s_score,s4.s_score,ROUND(sum(s5.s_score)/3,2) as avgs from student s1
left join score s2 on s1.s_id=s2.s_id and s2.c_id='01'
left join score s3 on s1.s_id=s3.s_id and s3.c_id='02'
left join score s4 on s1.s_id=s4.s_id and s4.c_id='03'
left join score s5 on s1.s_id=s5.s_id GROUP BY s1.s_name ORDER BY avgs DESC;
#查詢平均分,直接使用avg會不計算空值,導致空值沒有計算到


select c1.c_id,c1.c_name,MAX(s1.s_score) AS 最高分,MIN(s1.s_score) AS 最低分,avg(s1.s_score) as 平均分,sum(if(s1.s_score<60,1,0)) as 不及格人數(shù),(sum(if(s1.s_score<60,1,0))/COUNT(s1.s_score))
as 及格率
from course c1
left join score s1 on c1.c_id=s1.c_id
GROUP BY c_id,c_name;
#查詢最高分,最低分,平均分,及格率,此處將null值默認不計算

set @i=0;
SELECT a.*,(@i:=@i+1) i from(
SELECT s1.s_name,s2.s_score as 課程1,s3.s_score as 課程2,s4.s_score as 課程3,sum(s5.s_score) as 總分 from student s1
left join score s2 on s1.s_id=s2.s_id and s2.c_id='01'
left join score s3 on s1.s_id=s3.s_id and s3.c_id='02'
left join score s4 on s1.s_id=s4.s_id and s4.c_id='03'
left join score s5 on s1.s_id=s5.s_id GROUP BY s1.s_name ORDER BY 總分 desc) as a;
#查詢分數(shù),由高到低排序,添加序號


SELECT t_name,avg(s_score) as avgScore from score s1
left join course c1 on s1.c_id=c1.c_id
left join teacher t1 on c1.t_id=t1.t_id
group by t1.t_name ORDER BY avgScore desc;
#不同老師教的學生平均分

SELECT s1.*,c1.c_name,s2.s_score from student s1
left join score s2 on s1.s_id=s2.s_id
left join course c1 on s2.c_id=c1.c_id
where (s1.s_id in (SELECT s_id from (SELECT s_id from score s3 inner join course c2 on s3.c_id=c2.c_id and c2.c_id='01' ORDER BY s3.s_score desc LIMIT 1,2) as tt) and c1.c_id='01')
or (s1.s_id in (SELECT s_id from (SELECT s_id from score s3 inner join course c2 on s3.c_id=c2.c_id and c2.c_id='02' ORDER BY s3.s_score desc LIMIT 1,2) as ttt) and c1.c_id='02')
or (s1.s_id in (SELECT s_id from (SELECT s_id from score s3 inner join course c2 on s3.c_id=c2.c_id and c2.c_id='03' ORDER BY s3.s_score desc LIMIT 1,2) as tttt) and c1.c_id='03')
order by c_name,s_score desc;
#查詢各科第二名到第三名的學生信息

SELECT c_id as 課程編號,(sum(if(s_score>=85&&s_score<=100,1,0))/count(s_score)) as 優(yōu)秀,(sum(if(s_score>=70&&s_score<85,1,0))/count(s_score)) as 優(yōu)良,(sum(if(s_score>=60&&s_score<70,1,0))/
count(s_score)) as 良,(sum(if(s_score>=0&&s_score<60,1,0))/count(s_score)) asfrom score GROUP BY c_id;
#統(tǒng)計各科成績各分數(shù)段人數(shù):課程編號,課程名稱,[100-85],[85-70],[70-60],[0-60]及所占百分比

SELECT c_name,COUNT(s_id) from course c1
left join score s1 on s1.c_id=c1.c_id
GROUP BY c_name;#每一門課程選修的學生人數(shù)

?

?

select a.s_id,a.c_id,a.s_score from score a
 where (select COUNT(*) from score b where b.c_id=a.c_id and b.s_score>=a.s_score)<=2 order by a.c_id;#查詢課程前兩名
 
 SELECT s1.*,s2.s_score from student s1
 inner join score s2 on s1.s_id=s2.s_id 
 inner join course c1 on s2.c_id=c1.c_id
 inner join teacher t1 on t1.t_id=c1.t_id and t1.t_name="張三"
 order by s2.s_score desc LIMIT 0,1;
 #查詢選修"張三"老師所授課程的學生中,成績最高的學生信息及其成績 
 
 SELECT c_id,count(*) 總?cè)藬?shù) from score
 GROUP BY c_id HAVING 總?cè)藬?shù)>5
 ORDER BY 總?cè)藬?shù) desc,c_id;#查詢課程id和選修人數(shù)超過5人按總?cè)藬?shù)排序
 
 SELECT s1.* from student s1
 left join score s2 on s1.s_id=s2.s_id
 GROUP BY s1.s_id HAVING count(*)>=(select count(*) from course);#查詢選修了全部課程的學生信息
 
 SELECT s1.*,TIMESTAMPDIFF(YEAR,s1.s_birth,NOW()) as age from student s1;#查詢年齡

?

?

?

https://www.cnblogs.com/kangxinxin/p/11585935.html

本文摘自 :https://www.cnblogs.com/

開通會員,享受整站包年服務(wù)立即開通 >