赞
踩
首先这篇记录是根据下面B站的视频进行的简单记录!
因为是自己个人写的,所以有些SQL是自己的想法,可能会与视频中有出入,但个人认为有些SQL有几种写法,自己喜欢哪种写哪种,而且视频中只是讲了这些题目的实现逻辑,实际当中肯定要更加规范一点。
以下内容中如有错误,请各位大佬及时指出,并请不吝赐教!
视频中参考文章:
下载这个SQL文件新建一个数据库直接导入即可,sql语句太长就不往这放了
有些例题比较简单可以略过!
SELECT
Student.*,
01_score.CId,
1_score,
02_score.CId,
2_score
FROM
Student
JOIN ( SELECT SId, CId, score AS 1_score FROM SC WHERE CId = '01' ) AS 01_score ON Student.SId = 01_score.SId
JOIN ( SELECT SId, CId, score AS 2_score FROM SC WHERE CId = '02' ) AS 02_score ON 01_score.SId = 02_score.SId
WHERE
1_score > 2_score
SELECT
sc.SId,
avg( sc.score )
FROM
sc
GROUP BY
sc.SId
HAVING
avg( sc.score )> 60
SELECT
a.Sid,
count( sc.Cid ),
IFNULL( sum( sc.score ), 0 )
-- sum( CASE WHEN sc.score IS NULL THEN 0 ELSE sc.score END )
FROM
student AS a
LEFT JOIN sc ON sc.SId = a.Sid
GROUP BY
a.Sid
SELECT
count( Tid )
FROM
teacher
WHERE
Tname LIKE '张%'
SELECT student.SId, student.sname FROM student WHERE student.sid NOT IN ( SELECT sc.SId FROM sc INNER JOIN course ON sc.Cid = course.CId INNER JOIN teacher ON course.TId = teacher.TId WHERE teacher.Tname = '张三' )
SELECT
student.SId,
student.sname,
course.Cname,
teacher.Tname
FROM
sc
INNER JOIN course ON sc.Cid = course.CId
INNER JOIN teacher ON course.TId = teacher.TId
INNER JOIN student ON sc.SId = student.sid
WHERE
teacher.Tname = '张三'
ORDER BY
student.sid
SELECT
*
FROM
student
WHERE
student.sid IN (
SELECT
a.sid
FROM
( SELECT sid FROM sc WHERE sc.CId = '01' ) AS a
INNER JOIN ( SELECT sid FROM sc WHERE sc.CId = '02' ) AS b ON a.sid = b.sid
)
SELECT
sum( sc.score )
FROM
sc
WHERE
sc.cid = '02'
SELECT
a.sid,
student.sname
FROM
( SELECT sc.sid, count( sc.CId ) AS acount FROM sc WHERE sc.score < 60 GROUP BY sc.sid )
AS a
INNER JOIN
( SELECT sc.sid, count( sc.CId ) AS bcount FROM sc GROUP BY sc.sid )
AS b ON a.sid = b.sid
inner join student on student.sid = a.sid
WHERE
a.acount = b.bcount
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。