EXISTS和 NOT EXISTS 子查询 (高级查询 二)
Posted bingbing-deng
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了EXISTS和 NOT EXISTS 子查询 (高级查询 二)相关的知识,希望对你有一定的参考价值。
子查询:嵌入到另一个查询语句之中的查询语句
子查询注意事项:
1.子查询可以嵌套在sql语句中任何表达式出现的位置
2.只出现在子查询中没有出现在父查询中的表不能包含在输出列中
-----学生表
DROP TABLE IF EXISTS student;
CREATE TABLE `student`(
`studentNo` INT(4) NOT NULL COMMENT ‘学号‘,
`loginPwd` VARCHAR(20) NOT NULL COMMENT ‘密码‘,
`studentName` VARCHAR(50) NOT NULL COMMENT ‘学生姓名‘,
`sex` CHAR(2) DEFAULT ‘男‘ NOT NULL COMMENT ‘性别‘,
`gradeId` INT(4) UNSIGNED COMMENT ‘年级编号‘,
`phone` VARCHAR(50) COMMENT ‘联系电话‘,
`address` VARCHAR(255) COMMENT ‘地址‘,
`bornDate` DATETIME COMMENT ‘出生时间‘,
`email` VARCHAR(50) COMMENT‘邮件账号‘,
identityCard VARCHAR(18) COMMENT‘身份证号码‘,
PRIMARY KEY(`studentNo`)
);
-----年级表
DROP TABLE IF EXISTS grade;
CREATE TABLE `grade`(
gradeID INT(4) NOT NULL COMMENT ‘年级编号‘,
gradeName VARCHAR(50) NOT NULL COMMENT‘年级名称‘
);
-----科目表
DROP TABLE IF EXISTS subject;
CREATE TABLE `subject`(
subjectNo INT(4) NOT NULL COMMENT ‘课程编号‘ PRIMARY KEY, #主键标识列 自增1
subjectName VARCHAR(50) COMMENT ‘课程名称‘,
classHour INT(4) COMMENT ‘学时‘,
gradeID INT(4) COMMENT ‘年级编号‘
);
-----成绩表
DROP TABLE IF EXISTS result;
CREATE TABLE `result`(
`studentNo` INT(4) NOT NULL COMMENT ‘学号‘,
`subjectNo` INT(4) NOT NULL COMMENT ‘课程编号‘,
`examDate` DATETIME NOT NULL COMMENT ‘考试日期‘,
`studentResult` INT(4) NOT NULL COMMENT ‘考试成绩‘
);
-----插入年级表
INSERT INTO `grade` VALUES (‘1‘, ‘S1‘);
INSERT INTO `grade` VALUES (‘2‘, ‘S2‘);
INSERT INTO `grade` VALUES (‘3‘, ‘Y2‘);
-----插入成绩表
INSERT INTO `result` VALUES (‘10000‘, ‘1‘, ‘2016-02-15 00:00:00‘, ‘71‘);
INSERT INTO `result` VALUES (‘10000‘, ‘1‘, ‘2016-02-17 00:00:00‘, ‘60‘);
INSERT INTO `result` VALUES (‘10001‘, ‘1‘, ‘2016-02-17 00:00:00‘, ‘46‘);
INSERT INTO `result` VALUES (‘10002‘, ‘1‘, ‘2016-02-17 00:00:00‘, ‘83‘);
INSERT INTO `result` VALUES (‘10003‘, ‘1‘, ‘2016-02-17 00:00:00‘, ‘60‘);
INSERT INTO `result` VALUES (‘10004‘, ‘1‘, ‘2016-02-17 00:00:00‘, ‘60‘);
INSERT INTO `result` VALUES (‘10005‘, ‘1‘, ‘2016-02-17 00:00:00‘, ‘95‘);
INSERT INTO `result` VALUES (‘10006‘, ‘1‘, ‘2016-02-17 00:00:00‘, ‘93‘);
INSERT INTO `result` VALUES (‘10007‘, ‘1‘, ‘2016-02-17 00:00:00‘, ‘23‘);
----插入学生表
INSERT INTO `student` VALUES (‘10000‘, ‘123‘, ‘郭靖‘, ‘男‘, ‘1‘, ‘13645667783‘, ‘天津市河西区‘, ‘1990-09-08 00:00:00‘, null, null);
INSERT INTO `student` VALUES (‘10001‘, ‘123‘, ‘李文才‘, ‘男‘, ‘1‘, ‘13645667890‘, ‘地址不详‘, ‘1994-04-12 00:00:00‘, null, null);
INSERT INTO `student` VALUES (‘10002‘, ‘123‘, ‘李斯文‘, ‘男‘, ‘1‘, ‘13645556793‘, ‘河南洛阳‘, ‘1993-07-23 00:00:00‘, null, null);
INSERT INTO `student` VALUES (‘10003‘, ‘123‘, ‘张萍‘, ‘女‘, ‘1‘, ‘13642345112‘, ‘地址不详‘, ‘1995-06-10 00:00:00‘, null, null);
INSERT INTO `student` VALUES (‘10004‘, ‘123‘, ‘韩秋洁‘, ‘女‘, ‘1‘, ‘13812344566‘, ‘北京市海淀区‘, ‘1995-07-15 00:00:00‘, null, null);
INSERT INTO `student` VALUES (‘10005‘, ‘123‘, ‘张秋丽‘, ‘女‘, ‘1‘, ‘13567893246‘, ‘北京市东城区‘, ‘1994-01-17 00:00:00‘, null, null);
INSERT INTO `student` VALUES (‘10006‘, ‘123‘, ‘肖梅‘, ‘女‘, ‘1‘, ‘13563456721‘, ‘河北省石家庄市‘, ‘1991-02-17 00:00:00‘, null, null);
INSERT INTO `student` VALUES (‘10007‘, ‘123‘, ‘秦洋‘, ‘男‘, ‘1‘, ‘13056434411‘, ‘上海市卢湾区‘, ‘1992-04-18 00:00:00‘, null, null);
INSERT INTO `student` VALUES (‘10008‘, ‘123‘, ‘何晴晴‘, ‘女‘, ‘1‘, ‘13053445221‘, ‘广州市天河区‘, ‘1997-07-23 00:00:00‘, null, null);
INSERT INTO `student` VALUES (‘20000‘, ‘123‘, ‘王宝宝‘, ‘女‘, ‘2‘, ‘13318877954‘, ‘地址不详‘, ‘1995-09-10 00:00:00‘, null, null);
INSERT INTO `student` VALUES (‘20010‘, ‘123‘, ‘何小华‘, ‘女‘, ‘2‘, ‘13318877954‘, ‘地址不详‘, ‘1995-09-10 00:00:00‘, null, null);
INSERT INTO `student` VALUES (‘30011‘, ‘123‘, ‘陈志强‘, ‘女‘, ‘3‘, ‘13689965430‘, ‘地址不详‘, ‘1994-09-27 00:00:00‘, null, null);
INSERT INTO `student` VALUES (‘30012‘, ‘123‘, ‘李露露‘, ‘女‘, ‘3‘, ‘13685678854‘, ‘地址不详‘, ‘1992-09-27 00:00:00‘, null, null);
-----插入科目表
INSERT INTO `subject` VALUES (‘1‘, ‘Logic Java‘, ‘220‘, ‘1‘);
INSERT INTO `subject` VALUES (‘2‘, ‘html‘, ‘160‘, ‘1‘);
INSERT INTO `subject` VALUES (‘3‘, ‘Java OOP‘, ‘230‘, ‘2‘);
-------------------------------检查Logic Java 课程最近一次考试 。如果成绩达到80分
--------------------------------以上者,则显示分数排在前5,名学员和分数
#1.获取Logic java 课程编号
SELECT subjectNo FROM `subject` WHERE subjectName="Logic Java";
#2.获取ogic Java最近一次考试日期
SELECT MAX(examDate) FROM result WHERE subjectNo=(
SELECT subjectNo FROM `subject` WHERE subjectName="Logic Java");
#3.找到考试达到80分的记录
SELECT subjectNo,studentResult
FROM result
WHERE subjectNo=(SELECT subjectNo FROM `subject` WHERE subjectName =‘Logic Java‘)
AND examDate=(SELECT MAX(examDate) FROM result WHERE subjectNo=(
SELECT subjectNo FROM `subject` WHERE subjectName="Logic Java"))
AND studentResult >80
#4.显示Logic Java最近一次考试前5名的学生信息
SELECT subjectNo,studentResult
FROM result
WHERE EXISTS(
SELECT subjectNo,studentResult
FROM result
WHERE subjectNo=(SELECT subjectNo FROM `subject` WHERE subjectName =‘Logic Java‘)
AND examDate=(SELECT MAX(examDate) FROM result WHERE subjectNo=(
SELECT subjectNo FROM `subject` WHERE subjectName="Logic Java"))
AND studentResult >80
)
AND subjectNo =(SELECT subjectNo FROM `subject` WHERE subjectName="Logic Java")
AND examDate=(SELECT MAX(examDate) FROM result WHERE subjectNo=(
SELECT subjectNo FROM `subject` WHERE subjectName="Logic Java"))
ORDER BY studentResult DESC
LIMIT 5;
-------------------------------检查Logic Java 课程最近一次考试 。如果全部成绩未通过考试
--------------------------------(60分及格),认为本次考试偏难,计算的该次考试平均分加5分
#1.找到考试达到60分的记录
SELECT subjectNo,studentResult
FROM result
WHERE subjectNo=(SELECT subjectNo FROM `subject` WHERE subjectName =‘Logic Java‘)
AND examDate=(SELECT MAX(examDate) FROM result WHERE subjectNo=(
SELECT subjectNo FROM `subject` WHERE subjectName="Logic Java"))
AND studentResult >=60
#2.计算平均分加5分
SELECT AVG(studentResult)+5 AS 平均分
FROM result
WHERE NOT EXISTS(
SELECT subjectNo,studentResult
FROM result
WHERE subjectNo=(SELECT subjectNo FROM `subject` WHERE subjectName =‘Logic Java‘)
AND examDate=(SELECT MAX(examDate) FROM result WHERE subjectNo=(
SELECT subjectNo FROM `subject` WHERE subjectName="Logic Java"))
AND studentResult >=60
)
AND subjectNo =(SELECT subjectNo FROM `subject` WHERE subjectName="Logic Java")
AND examDate=(SELECT MAX(examDate) FROM result WHERE subjectNo=(
SELECT subjectNo FROM `subject` WHERE subjectName="Logic Java"));
-----。。。。。。
UPDATE result SET studentResult=50 WHERE subjectNo=1 AND examDate=‘2016-02-17‘
以上是关于EXISTS和 NOT EXISTS 子查询 (高级查询 二)的主要内容,如果未能解决你的问题,请参考以下文章
shell中$()和 ` `${}${!}${#}$[] 和$(()),[ ] 和(( ))和 [[ ]]
Java基础8---面向对象代码块和继承和this和super和重写和重载和final
Java基础8---面向对象代码块和继承和this和super和重写和重载和final
JS中some()和every()和join()和concat()和pop(),push(),shift(),unshfit()和map()和filter()
malloc和free,brk和sbrk和mmap和munmap的使用和关系以及内存分配的原理
Spring Security 'Roles' 和 'Privileges' 和 Thymeleaf 'hasRole' 和 'hasAuthority'