diff --git "a/40 \351\237\251\351\234\207\346\264\213/20230307 \346\237\245\350\257\242\344\275\234\344\270\232.md" "b/40 \351\237\251\351\234\207\346\264\213/20230307 \346\237\245\350\257\242\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..133926fe6419550d5f41566e5cae0faf7406f022 --- /dev/null +++ "b/40 \351\237\251\351\234\207\346\264\213/20230307 \346\237\245\350\257\242\344\275\234\344\270\232.md" @@ -0,0 +1,97 @@ +```mysql +# 笔记 + +子查询放在where,having后面 +单列单值直接用>,<,=等运算符 +单列多值用in,not in或搭配any使用 +子查询3种用法:select 子查询1 from 子查询2 where 子查询3 + + +create database student charset utf8; +use student; +CREATE TABLE stuinfo ( + stuNO VARCHAR(20) PRIMARY KEY, + stuName VARCHAR(20), + stuSex enum('男','女'), + stuAge INT, + stuAddress VARCHAR(20), + stuSeat INT +); +INSERT INTO stuinfo VALUES('s2501','张秋利','男',20,'美国硅谷',1),('s2502','李斯文','女',18,'湖北武汉',2),('s2503','马文才','男',22,'湖南长沙',3),('s2504','欧阳俊熊','女',21,'湖北武汉',4),('s2505','梅超风','男',20,'湖北武汉',5),('s2506','陈旋风','男',19,'美国硅谷',6); +UPDATE stuinfo set stuage = (18) WHERE stuNo = 's2503'; +CREATE TABLE stuExam( + examNo INT, + stuNo VARCHAR(20), + writtenExam INT, + labExam INT, + PRIMARY KEY(examNO,stuNo), + FOREIGN KEY (stuNo) REFERENCES stuinfo(stuNo) +); +INSERT INTO stuexam VALUES(1,'s2501',50,70),(2,'s2502',60,65),(3,'s2503',86,70),(4,'s2504',40,80),(5,'s2505',70,85),(6,'s2506',85,90); +CREATE TABLE stuMarks( + examNo INT PRIMARY KEY, + stuID VARCHAR(20), + score int, + FOREIGN KEY (examNo) REFERENCES stuExam(examNo) +); +ALTER TABLE stumarks ADD FOREIGN KEY (stuid) REFERENCES stuinfo(stuno); +INSERT INTO stumarks VALUES(1,'s2501',88),(2,'s2501',92),(3,'s2501',53),(4,'s2502',60),(5,'s2502',99),(6,'s2503',82); +-- 4.查询性别是男并且年龄大于等于20的学生的信息(用两种方法实现:普通查询和子查询) +SELECT * from stuinfo where stuage>=20 and stusex='男'; +SELECT * from (SELECT * from stuinfo where stuage>=20 and stusex='男') as acx; +-- 5.查询出年龄比所有男生年龄都大的女生的信息 + +select * from stuinfo where stuage>(SELECT max(stuage) from stuinfo where stusex='男') and stusex='女'; +-- + +-- 6.查询出所有选修课程都及格的学生的信息 (stuMarks) + +SELECT * from stumarks left join stuinfo on stumarks.stuid=stuinfo.stuno where stuid != any(select stuid from stumarks where score<60); +-- + +-- 7.查询出参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +select stuinfo.* from stumarks left join stuinfo on stumarks.stuid=stuinfo.stuno GROUP BY stuno; + +SELECT * from stuinfo where stuno in (SELECT stuid from stumarks); +-- + +-- 8.查询出没有参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +select stuinfo.* from stumarks right join stuinfo on stumarks.stuid=stuinfo.stuno where score is null GROUP BY stuno; + +SELECT * from stuinfo where stuno not in (SELECT stuid from stumarks); +-- + +-- 9.将有一门成绩成绩大于90分的学生的基本信息查询出来(stuMarks) + +SELECT stuinfo.* from stumarks left join stuinfo on stumarks.stuid=stuinfo.stuno where stuid = any(select stuid from stumarks where score>90) GROUP BY stuid; +-- + +-- 10.查询出平均成绩在80分以上的学生的基本信息(stuMarks) +select * from stuinfo where stuno in + +(SELECT stuid from stumarks WHERE score>80); +-- + +-- 11.查询出某同学所有考试成绩比“张秋利”同学所有分数都高的学生基本信息(stuMarks) +select stuinfo.* from stuinfo inner join stumarks on stumarks.stuid=stuinfo.stuno where score>( + +select max(score) from stuinfo inner join stumarks on stumarks.stuid=stuinfo.stuno where stuname ='张秋利') GROUP BY stuname; +-- + +-- 12.查询出某同学所有考试成绩只需要比“张秋利”同学某个分数高的学生基本信息(stuMarks) +select stuinfo.* from stuinfo inner join stumarks on stumarks.stuid=stuinfo.stuno where score> any( + +select score from stuinfo inner join stumarks on stumarks.stuid=stuinfo.stuno where stuname ='张秋利') and stuname != '张秋利' GROUP BY stuname; +-- + +-- 13.查询班上比所有男生年龄都要大的女生的信息 + +select * from stuinfo where stuage>(SELECT max(stuage) from stuinfo where stusex='男') and stusex='女'; +-- + +-- 14.查询出只是比某个男生年龄大的女生的信息 + +select * from stuinfo where stuage> any(SELECT stuage from stuinfo where stusex='男') and stusex='女'; +-- +``` +