标签:value 超过 就是 com core case 关联 curd char
https://blog.csdn.net/xiushuiguande/article/details/79476964
CREATE DATABASE IF NOT EXISTS SQL50; USE SQL50;
CREATE TABLE Student(sid VARCHAR(10),sname VARCHAR(10),sage DATETIME,ssex NVARCHAR(10)); 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‘ , ‘女‘);
CREATE TABLE Course(cid VARCHAR(10),cname VARCHAR(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);
Course课程表:
课程ID、课程名、教师ID
SC成绩表:
学生ID、课程ID、成绩
Student学生表:
学生ID、学生姓名、学生年龄、学生性别、
Teacher老师表:
教师ID、教师名
先查询 01课程的成绩表 & 02课程的成绩表
SELECT * FROM `sc` WHERE `cid` = 1; SELECT * FROM `sc` WHERE `cid` = 2;
使用连接查询,附加 01表的成绩 > 02表的成绩
SELECT sc1.`sid` FROM (SELECT `sid`,`cid`,`score` FROM `sc` WHERE `cid` = 1) AS sc1, (SELECT `sid`,`cid`,`score` FROM `sc` WHERE `cid` = 2) AS sc2 where sc1.`sid` = sc2.`sid` and sc1.`score` > sc2.`score`;
信息都在成绩表里面,要每个学生的全部成绩的平均成绩,
使用AVG聚合函数,然后对学生ID进行分组GROUP BY
查出上面的结果之后再对平均分进行 > 60的筛选
但是为什么SQL查不出结果???
SELECT `sid`,AVG(`score`) AS ‘avg_sc‘ FROM `sc` GROUP BY `sid` HAVING ‘avg_sc‘ > 60;
因为在成绩表里面,可以使用分组+计数+求和,求出成绩表内的学号,选课数和总成绩
然后再和学生表进行一个左连接,学生表作为主表,上面的结果表作为从表,因为还存在学生表没有选课的情况
成绩表的筛选
SELECT `sid`,COUNT(`cid`) AS ‘选课数‘,SUM(`score`) as ‘总成绩‘ FROM `sc` GROUP BY `sid`;
左连接学生表
SELECT S1.`sid`,S1.`sname`,S2.选课数,S2.总成绩 FROM `student` AS S1 LEFT JOIN ( SELECT `sid`,COUNT(`cid`) AS ‘选课数‘,SUM(`score`) AS ‘总成绩‘ FROM `sc`
GROUP BY `sid`) AS S2 ON S1.`sid` = S2.`sid`;
只在李老师表内查询,使用计数函数
SELECT COUNT(*) FROM `teacher` WHERE `tname` LIKE ‘李%‘;
先筛选张三老师教授的课程ID,可以看到是01课程
SELECT `tid` FROM `teacher` WHERE `tname` = ‘张三‘;
通过上面的线索,查找课程表,其对应的课程ID是02
select `cid` -- ,`cname`,`tid` from `course` where `tid` = (SELECT `tid` FROM `teacher` WHERE `tname` = ‘张三‘);
在成绩表查询没学02课程太难排查了,所以反过来,我们查学了02的学生ID
-- 我们查询学了该课程的学生ID SELECT DISTINCT `sid` FROM `sc` WHERE `cid` = (SELECT `cid` FROM `course` WHERE `tid` = (SELECT `tid` FROM `teacher` WHERE `tname` = ‘张三‘))
再用上面这张表连接学生表查询【左连接】
SELECT S1.`sid`,S1.`sname` FROM `student` AS S1 LEFT JOIN (SELECT DISTINCT `sid` FROM `sc` WHERE `cid` = (SELECT `cid` FROM `course` WHERE `tid` = (SELECT `tid` FROM `teacher` WHERE `tname` = ‘张三‘))) AS S2 ON S1.`sid` = S2.`sid` WHERE S1.`sid` NOT IN(S2.`sid`);
查询张三老师的教师编号:
SELECT `tid` FROM `Teacher` WHERE `tname` = ‘张三‘
然后根据编号查询所授课程:
SELECT `cid` FROM `Course` WHERE `tid` = (SELECT `tid` FROM `Teacher` WHERE `tname` = ‘张三‘);
然后得到对应的所学课程的学生编号
SELECT `sid` FROM `SC` WHERE `cid` IN( SELECT `cid` FROM `Course` WHERE `tid` = ( SELECT `tid` FROM `Teacher` WHERE `tname` = ‘张三‘ ) );
最后再联合学生表:
SELECT `sid`,`sname` FROM `Student` WHERE `sid` IN ( select `sid` from `SC` where `cid` IN( SELECT `cid` FROM `Course` WHERE `tid` = ( SELECT `tid` FROM `Teacher` WHERE `tname` = ‘张三‘ ) ) );
也就是说只要其中一个课程成绩大于60就不算在内
所以就可以先查询大于60分,再取反就行了
大于60分的学生ID:
SELECT `sid` FROM `SC` WHERE `score` > 60
然后取反得到:
SELECT `sid`,`sname` FROM `Student` WHERE `sid` NOT IN(SELECT `sid` FROM `SC` WHERE `score` > 60)
首先是需要知道全部课程个数
然后这个查询的意思是,计数学生学习课程的个数,如果小于总课数即没学全
但是需要跟学生表关联,他这里的联动真的难理解。。。
SELECT `sid`,`sname` FROM `Student` WHERE (SELECT COUNT(*) FROM `SC` WHERE `Student`.`sid` = `SC`.`sid`) < (SELECT COUNT(*) FROM `Course`)
SELECT a.sid, a.avg_score, SUM(CASE WHEN a.avg_score<b.avg_score THEN 1 ELSE 0 END)+1 scc FROM (SELECT sid,AVG(score) avg_score FROM SC GROUP BY sid) a INNER JOIN (SELECT sid,AVG(score) avg_score FROM SC GROUP BY sid) b GROUP BY a.sid, a.avg_score ORDER BY scc
SELECT `cid`,COUNT(`sid`) ‘人数‘ FROM `sc` GROUP BY `cid`
# 查询选课数只有1的学生编号 SELECT `sid`,COUNT(`cid`) ‘选课数‘ FROM `sc` GROUP BY `sid` HAVING `选课数` = 1 # 然后关联学生表进行查询 SELECT `student`.`sname`,`ss`.`sid` FROM `student`, (SELECT `sid`,COUNT(`cid`) ‘选课数‘ FROM `sc` GROUP BY `sid` HAVING `选课数` = 1) `ss` WHERE `student`.`sid` IN (`ss`.`sid`)
SELECT `ssex`,COUNT(1) ‘人数‘ FROM `student` GROUP BY `ssex`
SELECT * FROM `student` WHERE `sname` LIKE ‘%风%‘;
SELECT `sname`,COUNT(1) AS ‘同名人数‘ FROM `student` GROUP BY `sname` HAVING `同名人数` > 1
SELECT `sname`,`ssex`, COUNT(1) AS num FROM Student GROUP BY `sname`,`ssex` HAVING COUNT(1) > 1 AND `ssex`=‘男‘
SELECT * FROM `student` WHERE YEAR(`sage`) = 1990 ;
# 对课程编号分组,求平均分,然后分数升序排列,对课程编号降序排列 SELECT `cid`,AVG(`score`) FROM `sc` GROUP BY `cid` ORDER BY `cid` DESC;
# 在成绩表中的查询 SELECT * FROM `sc` WHERE `score` < 60 ORDER BY `cid` DESC; # 关联课程表 SELECT `sc`.*, `course`.`cname` FROM `sc`,`course` WHERE `sc`.`cid` = `course`.`cid` AND `sc`.`score` < 60 ORDER BY `sc`.`cid` DESC
# 1、现在成绩表查询课程编号为1的 SELECT * FROM `sc` WHERE `cid` = 01; # 2、再添加分数要求大于60的 SELECT * FROM `sc` WHERE `cid` = 01 AND `score` > 60; # 3、最后联合学生表获取名字 SELECT `sc`.*,`student`.`sname` FROM `sc`,`student` WHERE `sc`.`sid` = `student`.`sid` AND `sc`.`cid` = 01 AND `score` > 60
# 查询老师编号 SELECT `tid` FROM `teacher` WHERE `tname` = ‘张三‘; # 通过编号查询所授课程编号 SELECT `cid` FROM `course` WHERE `tid` = (SELECT `tid` FROM `teacher` WHERE `tname` = ‘张三‘); # 查询到成绩表学生编号和成绩 SELECT `sid`,`score` FROM `sc` WHERE`cid` = (SELECT `cid` FROM `course` WHERE `tid` = (SELECT `tid` FROM `teacher` WHERE `tname` = ‘张三‘)) ORDER BY `score` DESC LIMIT 1 # 关联学生表 SELECT `student`.`sname`,`ss`.`score` FROM `student`, (SELECT `sid`,`score` FROM `sc` WHERE`cid` = (SELECT `cid` FROM `course` WHERE `tid` = (SELECT `tid` FROM `teacher` WHERE `tname` = ‘张三‘)) ORDER BY `score` DESC LIMIT 1) `ss` WHERE `student`.`sid` = `ss`.`sid`
# 查询成绩表,按照课程字段分组,计数整个表的记录 # 要求课程大于5记录的倒序排序,并返回人数记录 SELECT cid,COUNT(1) COUNT FROM SC GROUP BY cid HAVING COUNT(1)>=5 ORDER BY COUNT DESC,cid ;
# 查询成绩表,并计数课程字段 # 对学号字段分组,之后计数的课程字段大于1的记录返回 SELECT `sid`,COUNT(`cid`) AS C FROM `sc` GROUP BY `sid` HAVING C > 1;
# 1 先查询课程表的总课程数量 SELECT COUNT(1) FROM Course # 2 在成绩表中,查询选课累计等于总课程数的学生ID SELECT sid ,COUNT(cid) count_c FROM SC GROUP BY sid HAVING count_c=(SELECT COUNT(1) FROM Course); # 3 查询学生表得到信息 SELECT `student`.* from `student`, (SELECT sid ,COUNT(cid) count_c FROM SC GROUP BY sid HAVING count_c=(SELECT COUNT(1) FROM Course)) as t WHERE `student`.`sid` in (t.`sid`)
# 用当前日期求得年份值,再减去字段日期的年份值即可 SELECT Student.*,YEAR(CURDATE())-YEAR(Student.sage) AS ‘年龄‘ FROM Student;
SELECT * FROM Student WHERE WEEKOFYEAR(NOW())-WEEKOFYEAR(sage)=0;
# 使用周按年换算函数,然后和月一样,下一周就取-1 SELECT * FROM Student WHERE WEEKOFYEAR(NOW())-WEEKOFYEAR(sage)=-1;
# 先求出现在的时间的月,再和字段月相减得为0的记录即可 SELECT * FROM Student WHERE MONTH(NOW())-MONTH(sage)=0;
# 本月值,减去字段月的值,相差一个月 # 因为本月大于字段月相减求得的差一定是大于0的 # 所以要查出下一个月过生日的人应该为负数,例如下一个月就-1,下3个月就-3 SELECT * FROM Student WHERE MONTH(NOW())-MONTH(sage)=-1;
【DataBase】SQL50 Training 50题训练
标签:value 超过 就是 com core case 关联 curd char
原文地址:https://www.cnblogs.com/mindzone/p/12808403.html