MySQL练习

Posted 小明分享圈

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL练习相关的知识,希望对你有一定的参考价值。

mysql是一种开放源代码的关系型数据库管理系统(RDBMS),使用最常用的数据库管理语言--结构化查询语言(SQL)进行数据库管理,MySQL因为其速度、可靠性和适应性而备受关注。

MySQL创建表

 1#创建Student表,并插入数据
2create table Student(SId varchar(10),Sname varchar(10),Sage datetime,Ssex varchar(10));
3insert into Student values('01' , '赵雷' , '1990-01-01' , '男');
4insert into Student values('02' , '钱电' , '1990-12-21' , '男');
5insert into Student values('03' , '孙风' , '1990-05-20' , '男');
6insert into Student values('04' , '李云' , '1990-08-06' , '男');
7insert into Student values('05' , '周梅' , '1991-12-01' , '女');
8insert into Student values('06' , '吴兰' , '1992-03-01' , '女');
9insert into Student values('07' , '郑竹' , '1989-07-01' , '女');
10insert into Student values('09' , '张三' , '2017-12-20' , '女');
11insert into Student values('10' , '李四' , '2017-12-25' , '女');
12insert into Student values('11' , '李四' , '2017-12-30' , '女');
13insert into Student values('12' , '赵六' , '2017-01-01' , '女');
14insert into Student values('13' , '孙七' , '2018-01-01' , '女');
15#创建科目表Course,并插入数据
16create table Course(CId varchar(10),Cname nvarchar(10),TId varchar(10))
17insert into Course values('01' , '语文' , '02');
18insert into Course values('02' , '数学' , '01');
19insert into Course values('03' , '英语' , '03');
20#创建教师表Teacher,并插入数据
21create table Teacher(TId varchar(10),Tname varchar(10))
22insert into Teacher values('01' , '张三');
23insert into Teacher values('02' , '李四');
24insert into Teacher values('03' , '王五');
25#创建成绩表SC,并插入数据
26create table SC(SId varchar(10),CId varchar(10),score decimal(18,1))
27insert into SC values('01' , '01' , 80);
28insert into SC values('01' , '02' , 90);
29insert into SC values('01' , '03' , 99);
30insert into SC values('02' , '01' , 70);
31insert into SC values('02' , '02' , 60);
32insert into SC values('02' , '03' , 80);
33insert into SC values('03' , '01' , 80);
34insert into SC values('03' , '02' , 80);
35insert into SC values('03' , '03' , 80);
36insert into SC values('04' , '01' , 50);
37insert into SC values('04' , '02' , 30);
38insert into SC values('04' , '03' , 20);
39insert into SC values('05' , '01' , 76);
40insert into SC values('05' , '02' , 87);
41insert into SC values('06' , '01' , 31);
42insert into SC values('06' , '03' , 34);
43insert into SC values('07' , '02' , 89);
44insert into SC values('07' , '03' , 98);

MySQL练习题目

MySQL练习

 1#查询" 01 “课程比” 02 "课程成绩高的学生的信息及课程分数
2select distinct a.*,d.cname,b.score from student a,
3(select * from sc where cid="01"as b,
4(select * from sc where cid="02"as c,course as d 
5where b.score>c.score and b.cid=d.cid and a.sid=b.sid and b.sid=c.sid;
6#查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩
7select a.sid,a.sname,avg(score) from sc,student as a
8where a.sid=sc.sid group by sid having avg(score)>60;
9#查询在 SC 表存在成绩的学生信息
10select distinct a.* from student as a,
11(select * from sc where score is not NULL) as b 
12where a.Sid=b.Sid;
13#查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null )
14select a.sid,a.sname,b.num,b.summ from student as a 
15left join (select sid,count(cid) as num,sum(score) as summ from sc group by sid) as b on a.sid=b.sid;    
16#查询「李」姓老师的数量
17select count(tname) from teacher where Tname like "李%";
18#查询学过「张三」老师授课的同学的信息
19select distinct a.* from student as a,sc as b,course as c,teacher as d where d.tname="张三" and d.tid=c.tid and c.cid=b.cid and b.sid=a.sid;
20#查询没有学全所有课程的同学的信息
21select a.* from sc as b,student as a 
22where a.sid=b.sid 
23group by b.sid 
24having count(cid)<(select count(cid) from course);
25或者
26select * from student where sid in(select sid from sc group by sid having count(cid)<3);
27#查询和" 01 "号的同学学习的课程完全相同的其他同学的信息
28select e.* from student as e,(select a.sid from (select distinct * from sc where cid in (select distinct cid from sc where sc.sid=01)) as a inner join (select * from sc group by sid having count(cid)=(select count(cid) from sc where sc.sid=01)) as b  on a.cid=b.cid and a.sid=b.sid) as f where e.sid=f.sid and and e.sid!=01;
29#查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息.
30select a.* from student as a,
31(select distinct sid from sc where cid in (select distinct cid from sc where sid=01)) as b where a.sid=b.sid and a.sid!=01;
32#查询没学过"张三"老师讲授的任一门课程的学生姓名
33select * from student where sid not in 
34(select a.sid from sc as b,teacher as d,course as c,student as a where d.tname="张三" and d.tid=c.tid and c.cid=b.cid and b.sid=a.sid);
35#查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
36select a.sid,a.sname,b.avg_score from student as a,
37(select sid,count(score) as num,avg(score) as avg_score from sc where score<60 group by sid) as b where a.sid=b.sid;
38#检索" 01 "课程分数小于 60,按分数降序排列的学生信息
39select student.* from student,sc where cid=01 and score<60 and sc.sid=student.sid order by sid desc;
40#按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
41select a.sid,a.avg_score,b.score01,c.score02,d.score03 from
42 (select sid,avg(score) as avg_score from sc group by sid order by avg(score) desc) as a 
43left join (select sid,score as score01 from sc where cid=01as b on a.sid=b.sid 
44left join (select sid,score as score02 from sc where cid=02as c on a.sid=c.sid 
45left join (select sid,score as score03 from sc where cid=03as d on a.sid=d.sid;
46#查询各科成绩最高分、最低分和平均分: 以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
47select a.cid,max(score),min(score),round(avg(score),2),count(score) as 选课人数,
48round(b.pass_01,2as 及格率,
49round(c.pass_02,2as 中等率,
50round(d.pass_03,2as 优良率,
51round(e.pass_04,2as 优秀率
52from sc as a
53left join (select sc.cid,x.num01/count(score) as pass_01 from sc,(select sc.cid,count(score) as num01 from sc where score>=60 group by sc.cid) as x where x.cid=sc.cid group by sc.cid) as b on a.cid=b.cid 
54left join (select sc.cid,y.num02/count(score) as pass_02 from sc,(select sc.cid,count(score) as num02 from sc where score between 70 and 80 group by sc.cid) as y where y.cid=sc.cid group by sc.cid) as c on a.cid=c.cid 
55left join (select sc.cid,z.num03/count(score) as pass_03 from sc,(select sc.cid,count(score) as num03 from sc where score between 80 and 90 group by sc.cid) as z where z.cid=sc.cid group by sc.cid) as d on a.cid=d.cid 
56left join (select sc.cid,u.num04/count(score) as pass_04 from sc,(select sc.cid,count(score) as num04 from sc where score>=90 group by sc.cid) as u where u.cid=sc.cid group by sc.cid) as e on a.cid=e.cid
57group by a.cid order by 选课人数 desc,cid asc;
58#按平均成绩进行排序,显示总排名和各科排名,Score 重复时保留名次空缺
59select a.sid,a.sname,b.avg_score as 平均成绩,b.rank_01 as 名次,c.rank_02 as 语文名次,d.rank_03 as 数学名次,e.rank_04 as 应许名次
60from student as a
61LEFT JOIN (select sid,avg(score) as avg_score,dense_rank() over(order by avg(score) desc) as rank_01 from sc group by sc.sid) as b on b.sid=a.sid
62LEFT JOIN (select sid,score,dense_rank() over(order by score desc) as rank_02 from sc where cid=01 group by sc.sid) as c on c.sid=a.sid
63LEFT JOIN (select sid,score,dense_rank() over(order by score desc) as rank_03 from sc where cid=02 group by sc.sid) as d on d.sid=a.sid
64LEFT JOIN (select sid,score,dense_rank() over(order by score desc) as rank_04 from sc where cid=03 group by sc.sid) as e on e.sid=a.sid
65group by a.sid order by 名次 asc;
66#查询各科成绩前三名的记录
67select * from (select *,dense_rank() over(partition by cid order by score desc) as num from sc) as a where a.num<=3;
68#查询 1990 年出生的学生名单
69select * from student where year(sage)=1990;
70#成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩
71select a.*,b.max(score) from student as a,sc as b,course as c,teacher as d where d.tname="张三" and d.tid=c.tid and c.cid=b.cid and b.sid=a.sid;
72#查询各学生的年龄,只按年份来算
73select sid,sname,year(now())-year(sage) as age from student;
74#查询下下个月过生日的学生
75select * from student where month(now())+2=month(sage);
76#统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比
77select distinct a.cid as 课程编号,c.cname as 课程名称,
78CONCAT(round(b.sect_01*10000/100,2),'%'as 85_100,
79CONCAT(round(b.sect_02*100,2),'%'as 70_85,
80CONCAT(round(b.sect_03*100,2),'%'as 60_70,
81CONCAT(round(b.sect_04*100,2),'%'as 0_60
82from sc as a,course as c,(
83select cid,sum(case when score>=85 then 1 else 0 end)/count(*) as  sect_01,
84sum(case when score>=70 and score<85 then 1 else 0 end)/count(*) as sect_02,
85sum(case when score>=60 and score<70 then 1 else 0 end)/count(*) as sect_03,
86sum(case when score<60 then 1 else 0 end)/count(*) as sect_04
87from sc group by cid) as b
88where a.cid=b.cid and b.cid=c.Cid;
MySQL笔记

  • 排序函数Rank() over()用法:对查出的结果集进行排序,当出现相同名次时排名一样,给下一位学生空出名次,类似排序函数:dense_rank() over(),给下一位学生不空出名次,row_num() over()连续排列,不考虑并列

  • 排序函数中partition by对结果集进行排序

  • 日期函数year,month,day,TIMESTAMPDIFF(interval,datetime1,datetime2)(日期或日期时间表达式之间的整数差)

  • MySql 中关键字 case when then else end 的用法

  • CONCAT函数是字符串拼接

  • case when then else end用法

1select      
2  case          -------------如果
3  when sex='1' then '男' -------------sex='1',则返回值'男'
4  when sex='2' then '女' -------------sex='2',则返回值'女'  
5  else 0         -------------其他的返回'其他’
6  end           -------------结束
7from student

加油少年,未来可期!!!

以上是关于MySQL练习的主要内容,如果未能解决你的问题,请参考以下文章

csharp Epicor标准练习片段

golang 去练习片段

部分代码片段

linux中怎么查看mysql数据库版本

ktor HTTP API 练习

从mysql的片段中加载ListView