@
SId 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别
CId 课程编号,Cname 课程名称,TId 教师编号
TId 教师编号,Tname 教师姓名
SId 学生编号,CId 课程编号,score 分数
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','钱电','1990-12-21','男');
insert into Student values ('03','孙风','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','女');
create table Course(CId varchar (10),Cname nvarchar(10),TId varchar (10));
insert into Course values ('01','语文','02');
insert into Course values ('02','数学','01');
insert into Course values ('03','英语','03');
create table Teacher(TId varchar (10),Tname varchar (10));
insert into Teacher values ('01','张三');
insert into Teacher values ('02','李四');
insert into Teacher values ('03','王五');
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 );
因为需要全部的学生信息,则需要在 SC 表中得到符合条件的 SId 后与 Student 表进行 join,可以 left join 也可以 right join。
# right join
SELECT *
FROM Student
RIGHT JOIN (
SELECT t1.SId, class1, class2
FROM (
SELECT SId, score AS class1
FROM sc
WHERE sc.CId = '01'
) t1, (
SELECT SId, score AS class2
FROM sc
WHERE sc.CId = '02'
) t2
WHERE t1.SId = t2.SId
AND t1.class1 > t2.class2
) r
ON Student.SId = r.SId;
# left join
SELECT *
FROM (
SELECT t1.SId, class1, class2
FROM (
SELECT SId, score AS class1
FROM sc
WHERE sc.CId = '01'
) t1, (
SELECT SId, score AS class2
FROM sc
WHERE sc.CId = '02'
) t2
WHERE t1.SId = t2.SId
AND t1.class1 > t2.class2
) r
LEFT JOIN Student ON Student.SId = r.SId;
SELECT *
FROM (
SELECT *
FROM sc
WHERE sc.CId = '01'
) t1, (
SELECT *
FROM sc
WHERE sc.CId = '02'
) t2
WHERE t1.SId = t2.SId;
这一道需要使用 join 的情况了," 02 " 课程可能不存在,即为 left join 的右侧或 right join 的左侧即可。
SELECT *
FROM (
SELECT *
FROM sc
WHERE sc.CId = '01'
) t1
LEFT JOIN (
SELECT *
FROM sc
WHERE sc.CId = '02'
) t2
ON t1.SId = t2.SId;
SELECT *
FROM (
SELECT *
FROM sc
WHERE sc.CId = '02'
) t2
RIGHT JOIN (
SELECT *
FROM sc
WHERE sc.CId = '01'
) t1
ON t1.SId = t2.SId;
SELECT *
FROM sc
WHERE sc.SId NOT IN (
SELECT SId
FROM sc
WHERE sc.CId = '01'
)
AND sc.CId = '02';
这里只需根据学生 ID 把成绩分组,对分组中的 score 求平均值,最后在选取结果中 AVG 大于 60 的即可。
注意,必须要给计算得到的 AVG 结果一个 alias(AS ss),得到学生信息的时候既可以用 join 也可以用一般的联合搜索。
SELECT student.SId, sname, ss
FROM student, (
SELECT SId, AVG(score) AS ss
FROM sc
GROUP BY SId
HAVING AVG(score) > 60
) r
WHERE student.sid = r.sid;
SELECT Student.SId, Student.Sname, r.ss
FROM Student
RIGHT JOIN (
SELECT SId, AVG(score) AS ss
FROM sc
GROUP BY SId
HAVING AVG(score) > 60
) r
ON Student.SId = r.SId;
SELECT s.SId, ss, Sname
FROM (
SELECT SId, AVG(score) AS ss
FROM sc
GROUP BY SId
HAVING AVG(score) > 60
) r
LEFT JOIN (
SELECT Student.SId, Student.Sname
FROM Student
) s
ON s.SId = r.SId;
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
GROUP BY sc.sid
) r
WHERE student.sid = r.sid;
如要显示没选课的学生(显示为 NULL),需要使用 join:
SELECT s.sid, s.sname, r.coursenumber, r.scoresum
FROM (
SELECT student.sid, student.sname
FROM student
) s
LEFT JOIN (
SELECT sc.sid, sum(sc.score) AS scoresum, count(sc.cid) AS coursenumber
FROM sc
GROUP BY sc.sid
) r
ON s.sid = r.sid;
这一题涉及到 in 和 exists 的用法,在这种小表中,两种方法的效率都差不多,但是请参考 SQL 查询中 in 和 exists 的区别分析,当表2的记录数量非常大的时候,选用 exists 比 in 要高效很多。exists 用于检查子查询是否至少会返回一行数据,该子查询实际上并不返回任何数据,而是返回值 True 或 False。
结论:IN() 适合B表比A表数据小的情况
结论:EXISTS() 适合B表比A表数据大的情况
SELECT *
FROM student
WHERE EXISTS (
SELECT sc.sid
FROM sc
WHERE student.sid = sc.sid
);
SELECT *
FROM student
WHERE student.sid IN (
SELECT sc.sid
FROM sc
);
SELECT count(*)
FROM teacher
WHERE tname LIKE '李%';
SELECT student.*
FROM student, teacher, course, sc
WHERE student.sid = sc.sid
AND course.cid = sc.cid
AND course.tid = teacher.tid
AND tname = '张三';
因为有学生什么课都没有选,反向思考,先查询选了所有课的学生,再选择这些人之外的学生。
SELECT *
FROM student
WHERE student.sid NOT IN (
SELECT sc.sid
FROM sc
GROUP BY sc.sid
HAVING count(sc.cid) = (
SELECT count(cid)
FROM course
)
);
这个用联合查询也可以,但是逻辑不清楚,我觉得较为清楚的逻辑是这样的:从 sc 表查询 01 同学的所有选课 cid -- 从 sc 表查询所有同学的 sid 如果其 cid 在前面的结果中 -- 从 student 表查询所有学生信息如果sid在前面的结果中。
SELECT *
FROM student
WHERE student.sid IN (
SELECT sc.sid
FROM sc
WHERE sc.cid IN (
SELECT sc.cid
FROM sc
WHERE sc.sid = '01'
)
);
仍然还是嵌套,三层嵌套, 或者多表联合查询:
SELECT *
FROM student
WHERE student.sid NOT IN (
SELECT sc.sid
FROM sc
WHERE sc.cid IN (
SELECT course.cid
FROM course
WHERE course.tid IN (
SELECT teacher.tid
FROM teacher
WHERE tname = '张三'
)
)
);
SELECT *
FROM student
WHERE student.sid NOT IN (
SELECT sc.sid
FROM sc, course, teacher
WHERE sc.cid = course.cid
AND course.tid = teacher.tid
AND teacher.tname = '张三'
);
从 SC 表中选取 score 小于 60 的,并 group by sid,having count 大于 1。
SELECT student.sid, student.sname, AVG(sc.score)
FROM student, sc
WHERE student.sid = sc.sid
AND sc.score < 60
GROUP BY sc.sid
HAVING count(*) > 1;
双表联合查询,在查询最后可以设置排序方式,语法为 ORDERBY*****DESC\ASC。
SELECT student.*, sc.score
FROM student, sc
WHERE student.sid = sc.sid
AND sc.score < 60
AND cid = '01'
ORDER BY sc.score DESC;
SELECT *
FROM sc
LEFT JOIN (
SELECT sid, avg(score) AS avscore
FROM sc
GROUP BY sid
) r
ON sc.sid = r.sid
ORDER BY avscore DESC;
以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率。
及格为>= 60 ,中等为:70 - 80 ,优良为:80 - 90 ,优秀为:>= 90
要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列。
SELECT sc.CId, max(sc.score) AS 最高分, min(sc.score) AS 最低分
, AVG(sc.score) AS 平均分, count(*) AS 选修人数
, sum(CASE
WHEN sc.score >= 60 THEN 1
ELSE 0
END) / count(*) AS 及格率
, sum(CASE
WHEN sc.score >= 70
AND sc.score < 80
THEN 1
ELSE 0
END) / count(*) AS 中等率
, sum(CASE
WHEN sc.score >= 80
AND sc.score < 90
THEN 1
ELSE 0
END) / count(*) AS 优良率
, sum(CASE
WHEN sc.score >= 90 THEN 1
ELSE 0
END) / count(*) AS 优秀率
FROM sc
GROUP BY sc.CId
ORDER BY count(*) DESC, sc.CId ASC
这一道题可以用变量,但也有更为简单的方法,即自交(左交),用 sc 中的 score 和自己进行对比,来计算“比当前分数高的分数有几个。
SELECT a.cid, a.sid, a.score
, count(b.score) + 1 AS rank
FROM sc a
LEFT JOIN sc b
ON a.score < b.score
AND a.cid = b.cid
GROUP BY a.cid, a.sid, a.score
ORDER BY a.cid, rank ASC;
这里主要学习一下使用变量。在 SQL 里面变量用 @ 来标识。
SET @crank = 0;
SELECT q.sid, total, @crank := @crank + 1 AS rank
FROM (
SELECT sc.sid, sum(sc.score) AS total
FROM sc
GROUP BY sc.sid
ORDER BY total DESC
) q;
group by 以后的查询结果无法使用别名,所以不要想着先单表 group by 计算出结果再从第二张表里添上课程信息,而应该先将两张表 join 在一起得到所有想要的属性再对这张总表进行统计计算。这里就不算百分比了,道理相同。
注意一下,用 case when 返回 1 以后的统计不是用 count 而是 sum。
SELECT course.cname, course.cid
, sum(CASE
WHEN sc.score <= 100
AND sc.score > 85
THEN 1
ELSE 0
END) AS "[100-85]"
, sum(CASE
WHEN sc.score <= 85
AND sc.score > 70
THEN 1
ELSE 0
END) AS "[85-70]"
, sum(CASE
WHEN sc.score <= 70
AND sc.score > 60
THEN 1
ELSE 0
END) AS "[70-60]"
, sum(CASE
WHEN sc.score <= 60
AND sc.score > 0
THEN 1
ELSE 0
END) AS "[60-0]"
FROM sc
LEFT JOIN course ON sc.cid = course.cid
GROUP BY sc.cid;
mysql 不能 group by了以后取 limit,所以不要想着讨巧了。思路有两种,第一种比较暴力,计算比自己分数大的记录有几条,如果小于 3 就 select,因为对前三名来说不会有 3 个及以上的分数比自己大了,最后再对所有 select 到的结果按照分数和课程编号排名即可。
SELECT *
FROM sc
WHERE (
SELECT count(*)
FROM sc a
WHERE sc.cid = a.cid
AND sc.score < a.score
) < 3
ORDER BY cid ASC, sc.score DESC;
第二种比较灵巧一些,用自身左交,但是有点难以理解。先用自己交自己,条件为 a.cid=b.cidanda.score<b.score,其实就是列出同一门课内所有分数比较的情况。想要查看完整的表可以:
SELECT *
FROM sc a
LEFT JOIN sc b
ON a.cid = b.cid
AND a.score < b.score
ORDER BY a.cid, a.score;
结果查看,发现结果是 47 行的一个表,列出了类似 01 号课里“ 30 分小于 50 ,也小于 70 ,也小于 80 ,也小于 90 ”“ 50 分小于 70 ,小于 80 ,小于 90 ”.....
所以理论上,对任何一门课来说,分数最高的那三个记录,在这张大表里,通过 a.sid 和 a.cid。
可以联合确定这个同学的这门课的这个分数究竟比多少个其他记录高/低,如果这个特定的 a.sid 和 a.cid 组合出现在这张表里的次数少于 3 个,那就意味着这个组合(学号+课号+分数)是这门课里排名前三的。
所以下面这个计算中 having count 部分其实 count() 或者任意其他列都可以,一个列只是因为比 count() 运行速度上更快。
SELECT a.sid, a.cid, a.score
FROM sc a
LEFT JOIN sc b
ON a.cid = b.cid
AND a.score < b.score
GROUP BY a.cid, a.sid
HAVING count(b.cid) < 3
ORDER BY a.cid;
SELECT cid, count(sid)
FROM sc
GROUP BY cid;
嵌套查询:
SELECT student.sid, student.sname
FROM student
WHERE student.sid IN (
SELECT sc.sid
FROM sc
GROUP BY sc.sid
HAVING count(sc.cid) = 2
);
联合查询:
SELECT student.SId, student.Sname
FROM sc, student
WHERE student.SId = sc.SId
GROUP BY sc.SId
HAVING count(*) = 2;
SELECT ssex, count(*)
FROM student
GROUP BY ssex;
SELECT *
FROM student
WHERE student.Sname LIKE '%风%'
找到同名的名字并统计个数:
SELECT sname, count(*)
FROM student
GROUP BY sname
HAVING count(*) > 1;
嵌套查询列出同名的全部学生的信息:
SELECT *
FROM student
WHERE sname IN (
SELECT sname
FROM student
GROUP BY sname
HAVING count(*) > 1
);
SELECT *
FROM student
WHERE YEAR(student.Sage) = 1990;
SELECT sc.cid, course.cname, AVG(SC.SCORE) AS average
FROM sc, course
WHERE sc.cid = course.cid
GROUP BY sc.cid
ORDER BY average DESC, cid ASC;
having 也可以用来截取结果表,在这里就先得到平均成绩总表,再截取 AVG 大于 85 的即可。
SELECT student.sid, student.sname, AVG(sc.score) AS aver
FROM student, sc
WHERE student.sid = sc.sid
GROUP BY sc.sid
HAVING aver > 85;
SELECT student.sname, sc.score
FROM student, sc, course
WHERE student.sid = sc.sid
AND course.cid = sc.cid
AND course.cname = '数学'
AND sc.score < 60;
SELECT student.sname, cid, score
FROM student
LEFT JOIN sc ON student.sid = sc.sid;
SELECT student.sname, course.cname, sc.score
FROM student, course, sc
WHERE sc.score > 70
AND student.sid = sc.sid
AND sc.cid = course.cid;
可以用 group by 来取唯一,也可以用 distinct。
SELECT cid
FROM sc
WHERE score < 60
GROUP BY cid;
SELECT DISTINCT sc.CId
FROM sc
WHERE sc.score < 60;
SELECT student.sid, student.sname
FROM student, sc
WHERE cid = '01'
AND score >= 80
AND student.sid = sc.sid;
SELECT sc.CId, count(*) AS 学生人数
FROM sc
GROUP BY sc.CId;
用 having max() 理论上也是对的,但是下面那种按分数排序然后取 limit 1 的更直观可靠。
SELECT student.*, sc.score, sc.cid
FROM student, teacher, course, sc
WHERE teacher.tid = course.tid
AND sc.sid = student.sid
AND sc.cid = course.cid
AND teacher.tname = '张三'
HAVING max(sc.score);
SELECT student.*, sc.score, sc.cid
FROM student, teacher, course, sc
WHERE teacher.tid = course.tid
AND sc.sid = student.sid
AND sc.cid = course.cid
AND teacher.tname = '张三'
ORDER BY score DESC
LIMIT 1;
这道题的思路继续上一题,我们已经查询到了符合限定条件的最高分了,这个时候只用比较这张表,找到全部 score 等于这个最高分的记录就可,看起来有点繁复。
SELECT student.*, sc.score, sc.cid
FROM student, teacher, course, sc
WHERE teacher.tid = course.tid
AND sc.sid = student.sid
AND sc.cid = course.cid
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 = '张三'
);
同上,在这里用了 inner join 后会有概念是重复的记录:“ 01 课与 03 课”=“ 03 课与 01课”,所以这里取唯一可以直接用 group by。
SELECT a.cid, a.sid, a.score
FROM sc a
INNER JOIN sc b
ON a.sid = b.sid
AND a.cid != b.cid
AND a.score = b.score
GROUP BY cid, sid;
SELECT a.sid, a.cid, a.score
FROM sc a
LEFT JOIN sc b
ON a.cid = b.cid
AND a.score < b.score
GROUP BY a.cid, a.sid
HAVING count(b.cid) < 2
ORDER BY a.cid;
SELECT sc.cid, count(sid) AS cc
FROM sc
GROUP BY cid
HAVING cc > 5;
SELECT sid, count(cid) AS cc
FROM sc
GROUP BY sid
HAVING cc >= 2;
SELECT student.*
FROM sc, student
WHERE sc.SId = student.SId
GROUP BY sc.SId
HAVING count(*) = (
SELECT DISTINCT count(*)
FROM course
)
一般都用 41 题的方法精确到天,按照出生日期来算,当前月日 <出生年月的月日则,年龄减一。
SELECT student.SId AS 学生编号, student.Sname AS 学生姓名
, TIMESTAMPDIFF(YEAR, student.Sage, CURDATE()) AS 学生年龄
FROM student
SELECT *
FROM student
WHERE WEEKOFYEAR(student.Sage) = WEEKOFYEAR(CURDATE());
SELECT *
FROM student
WHERE WEEKOFYEAR(student.Sage) = WEEKOFYEAR(CURDATE()) + 1;
SELECT *
FROM student
WHERE MONTH(student.Sage) = MONTH(CURDATE());
SELECT *
FROM student
WHERE MONTH(student.Sage) = MONTH(CURDATE()) + 1;
Copyright© 2013-2020
All Rights Reserved 京ICP备2023019179号-8