sql语句练习
表结构描述
1.学生表
Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别
2.课程表
Course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号
3.教师表
Teacher(t_id,t_name) –教师编号,教师姓名
4.成绩表
Score(s_id,c_id,s_score) –学生编号,课程编号,分数
建库
create database if not exists TestSchool charset utf8;
use TestSchool;
建表
学生表
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 Teachar( 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' , '数学' , '01');
insert into Course values('03' , '英语' , '03');
insert into Course values('04' , '物理' , '01');
教师表测试数据
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');
insert into Teacher values('04' , 'Lina');
insert into Teacher values('05' , 'PeterZhang');
成绩表测试数据
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('01' , '04' , 67);
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('02' , '04' , 98);
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('04' , '04' , 40);
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);
insert into Score values('08' , '01' , 89);
insert into Score values('08' , '02' , 98);
题目
1,查询"01"课程比"02"课程成绩高的学生的信息及课程分数
mysql> select b.*,
-> (select s_score from Score where s_id=a.s_id and c_id="01") as 01_score,
-> (select s_score from Score where s_id=a.s_id and c_id="02") as 02_score
-> from Score a left join Student b on a.s_id=b.s_id
-> group by a.s_id having 01_score>02_score;
+------+--------+------------+-------+----------+----------+
| s_id | s_name | s_birth | s_sex | 01_score | 02_score |
+------+--------+------------+-------+----------+----------+
| 02 | 钱电 | 1990-12-21 | 男 | 70 | 60 |
| 04 | 李云 | 1990-08-06 | 男 | 50 | 30 |
+------+--------+------------+-------+----------+----------+
2 rows in set (0.00 sec)
#复制验证:
select b.*, (select s_score from Score where s_id=a.s_id and c_id="01") as 01_score, (select s_score from Score where s_id=a.s_id and c_id="02") as 02_score from Score a left join Student b on a.s_id=b.s_id group by a.s_id having 01_score>02_score;
解读:mysql> select b.*, (4)
-> (select s_score from Score where s_id=a.s_id and c_id="01") as 01_score,
-> (select s_score from Score where s_id=a.s_id and c_id="02") as 02_score
-> from Score a left join Student b on a.s_id=b.s_id (1)
-> group by a.s_id (2)
-> having 01_score>02_score;(3)
划分一下大致的执行顺序就看的很清楚了
2.查询"01"课程比"02"课程成绩低的学生的信息及课程分数
mysql> select b.*,
->(select s_score from Score where s_id=a.s_id and c_id="01") as 01_score,
->(select s_score from Score where s_id=a.s_id and c_id="02") as 02_score
->from Score a left join Student b on a.s_id=b.s_id
->group by a.s_id having 01_score<02_score;
+------+--------+------------+-------+----------+----------+
| s_id | s_name | s_birth | s_sex | 01_score | 02_score |
+------+--------+------------+-------+----------+----------+
| 01 | 赵雷 | 1990-01-01 | 男 | 80 | 90 |
| 05 | 周梅 | 1991-12-01 | 女 | 76 | 87 |
| 08 | 王菊 | 1990-01-20 | 女 | 89 | 98 |
+------+--------+------------+-------+----------+----------+
3 rows in set (0.00 sec)
3.查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩 保留小数点
mysql> select b.s_id,b.s_name,avg(a.s_score) as avgscore
->from Score a left join Student b on a.s_id=b.s_id
->group by a.s_id having avgscore>=60;
+------+--------+----------+
| s_id | s_name | avgscore |
+------+--------+----------+
| 01 | 赵雷 | 84.0000 |
| 02 | 钱电 | 77.0000 |
| 03 | 孙风 | 80.0000 |
| 05 | 周梅 | 81.5000 |
| 07 | 郑竹 | 93.5000 |
| 08 | 王菊 | 93.5000 |
+------+--------+----------+
6 rows in set (0.00 sec)
4.查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩 (包括有成绩的和无成绩的)
mysql> select a.s_id, b.s_name, avg(a.s_score) as avgscore
-> from Score a,
-> Student b
-> where a.s_id = b.s_id
-> group by a.s_id
-> having avgscore < 60
-> union
-> select b.s_id, b.s_name, 0 as avgscore
-> from Student b
-> where b.s_id not in
-> (select distinct s_id from Score);
+------+--------+----------+
| s_id | s_name | avgscore |
+------+--------+----------+
| 04 | 李云 | 35.0000 |
| 06 | 吴兰 | 32.5000 |
+------+--------+----------+
2 rows in set (0.00 sec)
#复制验证:
select a.s_id, b.s_name, avg(a.s_score) as avgscore
from Score a,
Student b
where a.s_id = b.s_id
group by a.s_id
having avgscore < 60
union
select b.s_id, b.s_name, 0 as avgscore
from Student b
where b.s_id not in
(select distinct s_id from Score);
5.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
mysql> select b.s_id,b.s_name,count(a.c_id) as course_num,sum(a.s_score)as total_score
->from Score a right join Student b
->on a.s_id=b.s_id
->group by b.s_id;
+------+--------+------------+-------------+
| s_id | s_name | course_num | total_score |
+------+--------+------------+-------------+
| 01 | 赵雷 | 4 | 336 |
| 02 | 钱电 | 4 | 308 |
| 03 | 孙风 | 3 | 240 |
| 04 | 李云 | 4 | 140 |
| 05 | 周梅 | 2 | 163 |
| 06 | 吴兰 | 2 | 65 |
| 07 | 郑竹 | 2 | 187 |
| 08 | 王菊 | 2 | 187 |
+------+--------+------------+-------------+
8 rows in set (0.00 sec)
另一种写法:
mysql> select a.s_id, a.s_name, count(b.c_id) as course_count, sum(b.s_score) as score_sum from Student a left join Score b on a.s_id = b.s_id group by b.s_id;
ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'TestSchool.a.s_id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
#大致意思是说其中一个select的列没有在group by子句中并且包含非聚合列'TestSchool.a.s_id',s_id'在功能上不依赖于GROUP BY子句中的列;这与sql_mode=only_full_group_by不兼容。
#将group by后面的b.s_id改为a.s_id就好了,或者修改/etc/mysql/mysql.conf.d文件里sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,only_full_group_by,将only_full_group_by删掉重启mysql即可
mysql> select a.s_id, a.s_name, count(b.c_id) as course_count, sum(b.s_score) as score_sum from Student a left join Score b on a.s_id = b.s_id group by a.s_id;
+------+--------+--------------+-----------+
| s_id | s_name | course_count | score_sum |
+------+--------+--------------+-----------+
| 01 | 赵雷 | 4 | 336 |
| 02 | 钱电 | 4 | 308 |
| 03 | 孙风 | 3 | 240 |
| 04 | 李云 | 4 | 140 |
| 05 | 周梅 | 2 | 163 |
| 06 | 吴兰 | 2 | 65 |
| 07 | 郑竹 | 2 | 187 |
| 08 | 王菊 | 2 | 187 |
+------+--------+--------------+-----------+
8 rows in set (0.00 sec)
6.查询不姓"李"姓老师的数量
#使用模糊匹配
mysql> select count(t_id) from Teacher where t_name not like "李%";
+-------------+
| count(t_id) |
+-------------+
| 4 |
+-------------+
1 row in set (0.00 sec)
#使用正则匹配
mysql> select count(t_id) from Teacher where t_name not regexp "^李+";
+-------------+
| count(t_id) |
+-------------+
| 4 |
+-------------+
1 row in set (0.00 sec)
7.查询学过"张三"老师授课的同学的信息
mysql> select a.*,b.c_id
->from Student a inner join Score b
->on a.s_id=b.s_id and b.c_id
->in(select c_id from Course where t_id=(
select t_id from Teacher where t_name="张三")
);
+------+--------+------------+-------+------+
| s_id | s_name | s_birth | s_sex | c_id |
+------+--------+------------+-------+------+
| 01 | 赵雷 | 1990-01-01 | 男 | 02 |
| 01 | 赵雷 | 1990-01-01 | 男 | 04 |
| 02 | 钱电 | 1990-12-21 | 男 | 02 |
| 02 | 钱电 | 1990-12-21 | 男 | 04 |
| 03 | 孙风 | 1990-05-20 | 男 | 02 |
| 04 | 李云 | 1990-08-06 | 男 | 02 |
| 04 | 李云 | 1990-08-06 | 男 | 04 |
| 05 | 周梅 | 1991-12-01 | 女 | 02 |
| 07 | 郑竹 | 1989-07-01 | 女 | 02 |
| 08 | 王菊 | 1990-01-20 | 女 | 02 |
+------+--------+------------+-------+------+
10 rows in set (0.00 sec)
#统计选修张三老师课的各位同学选修了几门张三老师的课
mysql> select a.s_name,count(a.s_name) from Student a inner join Score b on a.s_id=b.s_id and b.c_id in(select c_id from Course where t_id=(select t_id from Teacher where t_name="张三")) group by a.s_id,a.s_name order by a.s_id;
+--------+-----------------+
| s_name | count(a.s_name) |
+--------+-----------------+
| 赵雷 | 2 |
| 钱电 | 2 |
| 孙风 | 1 |
| 李云 | 2 |
| 周梅 | 1 |
| 郑竹 | 1 |
| 王菊 | 1 |
+--------+-----------------+
7 rows in set (0.00 sec)
分层简化问题,先查询张三老师的教师编号,接着查询出课程编号,然后查询出选修了其中一门的学生编号 (思考,可以限定选修两门以上的学生),最后查询出学生信息
select st.*
from student st
where st.s_id in (select s.s_id
from score s
where s.c_id in(select c.c_id
from course c
where c.t_id = (select t.t_id from teacher t where t.t_name = "张三"))
group by s.s_id);
8、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
select s.*
from score b
join student s on b.s_id = s.s_id
where b.c_id = ‘01’
and b.s_id in(select a.s_id from score a where a.c_id = ‘02’);
select a.*
from student a,
score b,
score c
where a.s_id = b.s_id
and a.s_id = c.s_id
and b.c_id = ‘01’
and c.c_id = ‘02’;
9、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
/**
–1.学生表
Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别
–2.课程表
Course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号
–3.教师表
Teacher(t_id,t_name) –教师编号,教师姓名
–4.成绩表
Score(s_id,c_id,s_score) –学生编号,课程编号,分数
*/
select s.*
from score b
join student s on b.s_id = s.s_id
where b.c_id = ‘01’
and b.s_id not in(select a.s_id from score a where a.c_id = ‘02’);
select a.*
from student a
where a.s_id in (select s_id from score where c_id = ‘01’)
and a.s_id not in(select s_id from score where c_id = ‘02’);
10、查询没有学全所有课程的同学的信息
/**
–1.学生表
Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别
–2.课程表
Course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号
–3.教师表
Teacher(t_id,t_name) –教师编号,教师姓名
–4.成绩表
Score(s_id,c_id,s_score) –学生编号,课程编号,分数
*/
##方法一
select st.*
from score s
join student st on s.s_id = st.s_id
group by s.s_id
having count(s.c_id) < (select count(c_id) from course);
##方法二
select s.*
from student s
where s.s_id in(select s_id
from score
where s_id not in(select a.s_id
from score a
join score b on a.s_id = b.s_id and b.c_id = ‘02’
join score c on a.s_id = c.s_id and c.c_id = ‘03’
join score d on a.s_id = d.s_id and d.c_id = ‘04’
where a.c_id = ‘01’));
11、查询至少有一门课与学号为"03"的同学所学相同的同学的信息
/**
–1.学生表
Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别
–2.课程表
Course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号
–3.教师表
Teacher(t_id,t_name) –教师编号,教师姓名
–4.成绩表
Score(s_id,c_id,s_score) –学生编号,课程编号,分数
*/
select *
from student
where s_id in
(select distinct s_id from score where c_id in
(select c_id from score where s_id = ‘03’)
);
12、查询和"05"号的同学学习的课程完全相同的其他同学的信息
/**
–1.学生表
Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别
–2.课程表
Course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号
–3.教师表
Teacher(t_id,t_name) –教师编号,教师姓名
–4.成绩表
Score(s_id,c_id,s_score) –学生编号,课程编号,分数
*/
select st.*
from score s
join student st on st.s_id = s.s_id
where s.s_id not in
(select s_id from score where c_id not in(select c_id from score where s_id = ‘05’))
group by s.s_id
having s.s_id != '05’and count() = (select count() from score where s_id = ‘05’);
score表内容
s_id c_id s_score
01 01 80
01 02 90
01 03 99
01 04 67
02 01 70
02 02 60
02 03 80
02 04 98
03 01 80
03 02 80
03 03 80
04 01 50
04 02 30
04 03 20
04 04 40
05 01 76
05 02 87
06 01 31
06 03 34
07 02 89
07 03 98
08 01 89
08 02 98
05学生学习了01和02两门课程
加上c_id字段返回结果
(select s_id,c_id from score where c_id not in(select c_id from score where s_id = ‘05’))
1
语句查询的是该学生学习了05学习课程之外其他课程的学生信息。
在这里插入图片描述
not in 之后,剩余的学生只有两种情况:
学习了05全部课程
学习了05一部分课程
通过查询学习的课程数量,就能找到完全匹配的学生
13.查询没学过"张三"老师讲授的任一门课程的学生姓名
/**
–1.学生表
Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别
–2.课程表
Course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号
–3.教师表
Teacher(t_id,t_name) –教师编号,教师姓名
–4.成绩表
Score(s_id,c_id,s_score) –学生编号,课程编号,分数
*/
select s_name
from student
where s_id not in(select s_id
from score
where c_id in (select c_id from course where t_id =
(select t_id from teacher where t_name = ‘张三’))
group by s_id);
14、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
/**
–1.学生表
Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别
–2.课程表
Course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号
–3.教师表
Teacher(t_id,t_name) –教师编号,教师姓名
–4.成绩表
Score(s_id,c_id,s_score) –学生编号,课程编号,分数
*/
select st.s_id, st.s_name, round(avg(s.s_score), 2) as avgscore
from student st
join score s on st.s_id = s.s_id
where st.s_id in(select s_id from score where s_score < 60 group by s_id having count(*) >= 2)
group by st.s_id;
15、检索"01"课程分数小于60,按分数降序排列的学生信息(asc升序 desc降序)
/**
–1.学生表
Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别
–2.课程表
Course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号
–3.教师表
Teacher(t_id,t_name) –教师编号,教师姓名
–4.成绩表
Score(s_id,c_id,s_score) –学生编号,课程编号,分数
*/
select st.*, s.s_score
from student st
inner join score s on s.s_id = st.s_id
where s.c_id = ‘01’
and s_score < 60
order by s.s_score desc;
16、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
/**
–1.学生表
Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别
–2.课程表
Course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号
–3.教师表
Teacher(t_id,t_name) –教师编号,教师姓名
–4.成绩表
Score(s_id,c_id,s_score) –学生编号,课程编号,分数
*/
select s.s_id, s.c_id, s.s_score,
(select avg(s_score) from score where s_id = s.s_id group by s_id)as avgscore
from score s order by avgscore desc,s.s_id desc ,c_id;
17、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
/**
–1.学生表
Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别
–2.课程表
Course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号
–3.教师表
Teacher(t_id,t_name) –教师编号,教师姓名
–4.成绩表
Score(s_id,c_id,s_score) –学生编号,课程编号,分数
*/
select s.c_id as ‘课程ID’, c.c_name as ‘课程name’,max(s_score) as ‘最高分’,min(s_score) as ‘最低分’,
round(avg(s_score),2) as ‘平均分’,
ifnull(
concat(
round(
100*(select count() from score where c_id = s.c_id and s_score>=60 group by c_id ) /count()
)
,’%’)
,‘0’) as ‘及格率’,
ifnull(concat(round(100*(select count() from score where c_id = s.c_id and s_score>=70 and s_score<80 group by c_id )/count()),’%’),‘0’) as ‘中等率’,
ifnull(concat(round(100* (select count() from score where c_id = s.c_id and s_score>=80 and s_score<90 group by c_id )/count()),’%’),‘0’) as ‘优良率’,
ifnull(concat(round( 100*(select count() from score where c_id = s.c_id and s_score>=90 group by c_id )/count()),’%’),‘0’)as ‘优秀率’
from score s
join course c on s.c_id = c.c_id
group by s.c_id;
18 查询学生的总成绩并进行排名
/**
–1.学生表
Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别
–2.课程表
Course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号
–3.教师表
Teacher(t_id,t_name) –教师编号,教师姓名
–4.成绩表
Score(s_id,c_id,s_score) –学生编号,课程编号,分数
*/
select st.s_id,st.s_name,sum(s.s_score) total
from score s
join student st on s.s_id = st.s_id
group by s_id
order by total desc;
select a.s_id,
@i:=@i+1 as i,
@k:=(case when @score=a.sum_score then @k else @i end) as rank,
@score:=a.sum_score as score
from (select s_id,SUM(s_score) as sum_score from score GROUP BY s_id ORDER BY sum_score DESC)a,
(select @k:=0,@i:=0,@score:=0)s;
19查询不同老师所教不同课程平均分从高到低显示
/**
–1.学生表
Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别
–2.课程表
Course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号
–3.教师表
Teacher(t_id,t_name) –教师编号,教师姓名
–4.成绩表
Score(s_id,c_id,s_score) –学生编号,课程编号,分数
*/
select t.t_name, c.c_name,round(avg(s.s_score)) as avg
from score s
join course c
join teacher t on c.c_id = s.c_id and t.t_id = c.t_id
group by s.c_id order by avg desc;
20、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
/**
–1.学生表
Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别
–2.课程表
Course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号
–3.教师表
Teacher(t_id,t_name) –教师编号,教师姓名
–4.成绩表
Score(s_id,c_id,s_score) –学生编号,课程编号,分数
*/
(select st.* ,s.c_id,s.s_score from score s join student st on s.s_id = st.s_id
where s.c_id=‘01’ order by s.s_score desc limit 1,2)
union
(select st.* ,s.c_id,s.s_score from score s join student st on s.s_id = st.s_id
where s.c_id=‘02’ order by s.s_score desc limit 1,2)
union
(select st.* ,s.c_id,s.s_score from score s join student st on s.s_id = st.s_id
where s.c_id=‘03’ order by s.s_score desc limit 1,2)
union
(select st.* ,s.c_id ,s.s_score from score s join student st on s.s_id = st.s_id
where s.c_id=‘04’ order by s.s_score desc limit 1,2);
#方法二
select d.,c.排名,c.s_score,c.c_id from (
select a.s_id,a.s_score,a.c_id,@i:=@i+1 as 排名 from score a,(select @i:=0)s where a.c_id=‘01’
)c
left join student d on c.s_id=d.s_id
where 排名 BETWEEN 2 AND 3
UNION
select d.,c.排名,c.s_score,c.c_id from (
select a.s_id,a.s_score,a.c_id,@j:=@j+1 as 排名 from score a,(select @j:=0)s where a.c_id=‘02’
)c
left join student d on c.s_id=d.s_id
where 排名 BETWEEN 2 AND 3
UNION
select d.,c.排名,c.s_score,c.c_id from (
select a.s_id,a.s_score,a.c_id,@k:=@k+1 as 排名 from score a,(select @k:=0)s where a.c_id=‘03’
)c
left join student d on c.s_id=d.s_id
where 排名 BETWEEN 2 AND 3
UNION
select d.,c.排名,c.s_score,c.c_id from (
select a.s_id,a.s_score,a.c_id,@k:=@k+1 as 排名 from score a,(select @k:=0)s where a.c_id=‘04’
)c
left join student d on c.s_id=d.s_id
where 排名 BETWEEN 2 AND 3;
21 统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比
/**
–1.学生表
Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别
–2.课程表
Course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号
–3.教师表
Teacher(t_id,t_name) –教师编号,教师姓名
–4.成绩表
Score(s_id,c_id,s_score) –学生编号,课程编号,分数
*/
select s.c_id,c.c_name,count() as total,
@part1:=(select count(s_id) from score where c.c_id = c_id and s_score>85 and s_score<=100) as part11,
concat(round(@part1/ count() *100,2),’%’) as “[100-85]”,#断开连接后,每第一次查询计算的结果百分比为null,不知道是不是因为这时变量@part1还没初始化的原因?
@part2:=(select count(s_id) from score where c.c_id = c_id and s_score>70 and s_score<=85) as part22,
concat(round((select count(s_id) from score where c.c_id = c_id and s_score>70 and s_score<=85)/ count(*) *100,2),’%’) as “[85-70]”,
@part3:=(select count(s_id) from score where c.c_id = c_id and s_score>60 and s_score<=70) as part33,
concat(round(@part3/ count(*) *100,2),’%’) as “[70-60]”,
@part4:=(select count(s_id) from score where c.c_id = c_id and s_score>=0 and s_score<=60) as part44,
concat(round(@part4/ count(*) *100,2),’%’) as “[60-0]”
from score s join course c on s.c_id = c.c_id join (select @part1=0, @part2=0,@part3=0, @part4=0) b
group by s.c_id
22、查询学生平均成绩及其名次
/**
–1.学生表
Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别
–2.课程表
Course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号
–3.教师表
Teacher(t_id,t_name) –教师编号,教师姓名
–4.成绩表
Score(s_id,c_id,s_score) –学生编号,课程编号,分数
*/
select a.s_id,a.avg_s, @i:=@i+1 as ‘排名’
from
(select s_id, ROUND(AVG(s_score), 2) as avg_s from score GROUP BY s_id order by avg_s desc) a,
(select @i:=0)b;
select a.s_id,
@i:=@i+1 as ‘不保留空缺排名’,
@k:=(case when @avg_score=a.avg_s then @k else @i end) as ‘排名’,
@avg_score:=avg_s as ‘平均分’
from (select s_id,ROUND(AVG(s_score),2) as avg_s from score GROUP BY s_id order by avg_s desc)a,
(select @avg_score:=0,@i:=0,@k:=0)b;
23/查询各科成绩前三名的记录
/**
–1.学生表
Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别
–2.课程表
Course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号
–3.教师表
Teacher(t_id,t_name) –教师编号,教师姓名
–4.成绩表
Score(s_id,c_id,s_score) –学生编号,课程编号,分数
*/
select *
from score a
where (select count(*)
from score b
where a.c_id = b.c_id
and b.s_score > a.s_score) < 3 #>1表示第二名之后
order by a.c_id, a.s_score desc
#方法二
select a.s_id,a.c_id,a.s_score from score a
left join score b on a.c_id = b.c_id and a.s_score<b.s_score
group by a.s_id,a.c_id,a.s_score HAVING COUNT(b.s_id)❤️
ORDER BY a.c_id,a.s_score DESC
24 查询出只有两门课程的全部学生的学号和姓名
/**
–1.学生表
Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别
–2.课程表
Course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号
–3.教师表
Teacher(t_id,t_name) –教师编号,教师姓名
–4.成绩表
Score(s_id,c_id,s_score) –学生编号,课程编号,分数
*/
select s_id, s_name
from student
where s_id in(select s_id from score GROUP BY s_id HAVING COUNT(c_id) = 2);
25、查询男生、女生人数
/**
–1.学生表
Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别
–2.课程表
Course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号
–3.教师表
Teacher(t_id,t_name) –教师编号,教师姓名
–4.成绩表
Score(s_id,c_id,s_score) –学生编号,课程编号,分数
*/
select s_sex, COUNT(s_sex) as 人数
from student
GROUP BY s_sex
1
2
3
26、查询同名同性学生名单,并统计同名人数
/**
–1.学生表
Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别
–2.课程表
Course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号
–3.教师表
Teacher(t_id,t_name) –教师编号,教师姓名
–4.成绩表
Score(s_id,c_id,s_score) –学生编号,课程编号,分数
*/
select a.s_name, a.s_sex, count(*)
from student a
JOIN student b on a.s_id != b.s_id and a.s_name = b.s_name and a.s_sex = b.s_sex
GROUP BY a.s_name, a.s_sex
27 查询课程名称为"数学",且分数低于60的学生姓名和分数
/**
–1.学生表
Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别
–2.课程表
Course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号
–3.教师表
Teacher(t_id,t_name) –教师编号,教师姓名
–4.成绩表
Score(s_id,c_id,s_score) –学生编号,课程编号,分数
*/
select a.s_name, b.s_score
from score b
LEFT JOIN student a on a.s_id = b.s_id
where b.c_id = (select c_id from course where c_name = ‘数学’)
and b.s_score < 60;
28 查询所有学生的课程及分数情况;
/**
–1.学生表
Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别
–2.课程表
Course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号
–3.教师表
Teacher(t_id,t_name) –教师编号,教师姓名
–4.成绩表
Score(s_id,c_id,s_score) –学生编号,课程编号,分数
*/
select a.s_id,a.s_name,
#ifnull((select s_score from score where s_id = a.s_id and c_id=‘01’),‘0’)as ‘语文’,
SUM(case c.c_name when ‘语文’ then b.s_score else 0 end) as ‘语文’,
SUM(case c.c_name when ‘数学’ then b.s_score else 0 end) as ‘数学’,
SUM(case c.c_name when ‘英语’ then b.s_score else 0 end) as ‘英语’,
SUM(case c.c_name when ‘物理’ then b.s_score else 0 end) as ‘物理’,
SUM(b.s_score) as ‘总分’
from student a left join score b on a.s_id = b.s_id
left join course c on b.c_id = c.c_id
GROUP BY a.s_id,a.s_name;
29、查询任何一门课程成绩在70分以上的姓名、课程名称和分数;
/**
–1.学生表
Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别
–2.课程表
Course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号
–3.教师表
Teacher(t_id,t_name) –教师编号,教师姓名
–4.成绩表
Score(s_id,c_id,s_score) –学生编号,课程编号,分数
*/
select a.s_name, b.c_name, c.s_score
from course b
left join score c on b.c_id = c.c_id
left join student a on a.s_id = c.s_id
where c.s_score >= 70;
30 查询不及格的课程
/**
–1.学生表
Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别
–2.课程表
Course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号
–3.教师表
Teacher(t_id,t_name) –教师编号,教师姓名
–4.成绩表
Score(s_id,c_id,s_score) –学生编号,课程编号,分数
*/
select a.s_id, a.c_id, b.c_name, a.s_score
from score a
left join course b on a.c_id = b.c_id
where a.s_score < 60;
31查询课程编号为01且课程成绩在80分以上的学生的学号和姓名;
/**
–1.学生表
Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别
–2.课程表
Course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号
–3.教师表
Teacher(t_id,t_name) –教师编号,教师姓名
–4.成绩表
Score(s_id,c_id,s_score) –学生编号,课程编号,分数
*/
select s.s_id , st.s_name,s_score from score s join student st on s.s_id = st.s_id
where c_id=‘01’ and s_score>80;
1
2
32 查询选修"张三"老师所授课程(可能有多个课程)的学生中,成绩最高的学生信息及其成绩
/**
–1.学生表
Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别
–2.课程表
Course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号
–3.教师表
Teacher(t_id,t_name) –教师编号,教师姓名
–4.成绩表
Score(s_id,c_id,s_score) –学生编号,课程编号,分数
*/
select st.s_id, st.s_name, a.c_id, a.s_score
from score a
join (select s_id, c_id, max(s_score) as maxscore
from score
where c_id in(select c_id from course where t_id = (select t_id from teacher where t_name = ‘张三’))
group by c_id) b on a.c_id = b.c_id and a.s_score = b.maxscore
join student st on st.s_id = a.s_id;
33查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
/**
–1.学生表
Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别
–2.课程表
Course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号
–3.教师表
Teacher(t_id,t_name) –教师编号,教师姓名
–4.成绩表
Score(s_id,c_id,s_score) –学生编号,课程编号,分数
*/
select DISTINCT b.s_id, b.c_id, b.s_score
from score a,
score b
where a.c_id != b.c_id
and a.s_score = b.s_score;
34查询每门课程成绩最好的前两名
/**
–1.学生表
Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别
–2.课程表
Course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号
–3.教师表
Teacher(t_id,t_name) –教师编号,教师姓名
–4.成绩表
Score(s_id,c_id,s_score) –学生编号,课程编号,分数
*/
(select * from score where c_id = ‘01’ order by c_id ,s_score desc limit 0,2)
union
(select * from score where c_id = ‘02’ order by c_id ,s_score desc limit 2)
union
(select * from score where c_id = ‘03’ order by c_id ,s_score desc limit 2)
union
(select * from score where c_id = ‘04’ order by c_id ,s_score desc limit 2);
35、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
/**
–1.学生表
Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别
–2.课程表
Course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号
–3.教师表
Teacher(t_id,t_name) –教师编号,教师姓名
–4.成绩表
Score(s_id,c_id,s_score) –学生编号,课程编号,分数
*/
select c_id, count(s_id)
from score
group by c_id
having count(s_id) > 5
order by count(s_id) desc, c_id;
36查询各学生的年龄
– 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一
/**
–1.学生表
Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别
–2.课程表
Course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号
–3.教师表
Teacher(t_id,t_name) –教师编号,教师姓名
–4.成绩表
Score(s_id,c_id,s_score) –学生编号,课程编号,分数
*/
select s_birth, (DATE_FORMAT(NOW(), ‘%Y’) - DATE_FORMAT(s_birth, ‘%Y’) -
(case
when DATE_FORMAT(NOW(), ‘%m%d’) > DATE_FORMAT(s_birth, ‘%m%d’) then 0
else 1 end)) as age
from student;
DATE_FORMAT() 函数用于以不同的格式显示日期/时间数据。
date 参数是合法的日期。format 规定日期/时间的输出格式。
在这里插入图片描述
在这里插入图片描述
DATE_FORMAT(NOW(),’%b %d %Y %h:%i %p’)
DATE_FORMAT(NOW(),’%m-%d-%Y’)
DATE_FORMAT(NOW(),’%d %b %y’)
DATE_FORMAT(NOW(),’%d %b %Y %T:%f’)
结果类似:
Dec 29 2008 11:45 PM
12-29-2008
29 Dec 08
29 Dec 2008 16:25:46.635
37 查询本月过生日的学生
/**
–1.学生表
Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别
–2.课程表
Course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号
–3.教师表
Teacher(t_id,t_name) –教师编号,教师姓名
–4.成绩表
Score(s_id,c_id,s_score) –学生编号,课程编号,分数
*/
select * from student where MONTH(DATE_FORMAT(NOW(),’%Y%m%d’)) =MONTH(s_birth)
1
在这里插入图片描述
文档地址:https://www.yiibai.com/mysql/month.html
38、 查询本周过生日的学生
/**
–1.学生表
Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别
–2.课程表
Course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号
–3.教师表
Teacher(t_id,t_name) –教师编号,教师姓名
–4.成绩表
Score(s_id,c_id,s_score) –学生编号,课程编号,分数
*/
select * from student where WEEK(DATE_FORMAT(NOW(),’%Y%m%d’))=WEEK(s_birth)
select * from student where YEARWEEK(s_birth)=YEARWEEK(DATE_FORMAT(NOW(),’%Y%m%d’))
select DATE_FORMAT(NOW(),’%Y%m%d’)
知识补充1–连结查询
SQL join 用于根据两个或多个表中的列之间的关系,从这些表中查询数据
INNER JOIN(内连接),在表中存在至少一个匹配时,INNER JOIN 关键字返回行(注释:INNER JOIN 与 JOIN 是相同的。)
JOIN: 如果表中有至少一个匹配,则返回行
LEFT JOIN: 即使右表中没有匹配,也从左表返回所有的行
RIGHT JOIN: 即使左表中没有匹配,也从右表返回所有的行
FULL JOIN: 只要其中一个表中存在匹配,就返回行
写mysql语句查询语句的思路:先看问题涉及到那些列,再看看这些列在哪些表中,是否需要多表连结查询,然后看条件
知识补充2–sql书写顺序和
sql书写顺序
select <distinct> <select_list> <聚合函数等计算字段列表>
from
<table_name> <join_type> join <join_table>
on <join_condition>
where <where_condition>
group by <group_by_list>
having <having_condition>
order by <order_by_condition>
limit <limt_number>
sql执行顺序
(1)from
(3)<left table> <join_type> join <join_table>
(2)on <on_condition>
(4)where <where_condition>
(5)group by <group_by_list>
(6)sum()avg()等聚合函数
(7)having <having_condition>
(8)select <select_list>
(9)distinct
(10)order by <order_by_condition>
(11)limit <limit_number>
知识补充3–组合查询 union
利用UNION,可给出多条SELECT语句,将它们的结果组合成单个结果集 .
1,UNION中的每个查询必须包含相同的列、表达式或聚集函数(不过各个列不需要以相同的次序列出)
2,UNION默认自动将查询结果集去重--去除重复的行。如果想返回所有匹配行,可使用UNION ALL而不是UNION。