sql语句中如何用 EXISTS 引入子查询?没有exists引入子查询时怎样实现子查询?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql语句中如何用 EXISTS 引入子查询?没有exists引入子查询时怎样实现子查询?相关的知识,希望对你有一定的参考价值。
比如查询某个表中相同ID中登记时间最大的记录:select * from bb t where exists (selec * form bb where id=t.id and 登记时间<t.登记时间)
如果不用exists ,可以如下方式写:
select a.* from bb a,(select id,max(登记时间) as 登记时间 from bb group by id) b
where a.id=b.id and a.登记时间=b.登记时间 参考技术A (SQL语句) exists (语句),如果没有exists,还可用where ,join , not ,in 等等,SQL是很丰富的,这个要自己平常多去学习,具体用法也要多用才能掌握其中的奥妙。 参考技术B PublishDate 是一个字段
(SELECT TOP 1 * FROM articles WHERE SysCategory='2' ORDER BY PublishDate DESC) 是多个字段
一个字段和多个字段无法比较的.
要把
(SELECT TOP 1 * FROM articles WHERE SysCategory='2' ORDER BY PublishDate DESC)
改成
(SELECT TOP 1 PublishDate FROM articles WHERE SysCategory='2' ORDER BY PublishDate DESC)
参考技术C 我也想知道的
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‘
以上是关于sql语句中如何用 EXISTS 引入子查询?没有exists引入子查询时怎样实现子查询?的主要内容,如果未能解决你的问题,请参考以下文章
SQL - 当不使用 EXISTS 引入子查询时,选择列表中只能指定一个表达式
微软的sql2005中如何用SQL语句查询出将一个表的字段的说明
SQL 中删除语法的问题 - 当子查询没有与 EXISTS 一起引入时,错误只能在选择列表中指定一个表达式