子查询(Sub Query)或者说内查询(Inner Query),也可以称作嵌套查询(Nested Query),是一种嵌套在其他 SQL 查询的 WHERE 子句中的查询。
子查询用于为主查询返回其所需数据,或者对检索数据进行进一步的限制。
子查询可以在 SELECT、INSERT、UPDATE 和 DELETE 语句中,同 =、<、>、>=、<=、IN、BETWEEN 等运算符一起使用。
使用子查询必须遵循以下几个规则:
数据库名:BOOK
学生信息表:student
字段名称 数据类型 说明
stuID CHAR(10) 学生编号,主键
stuName VARCHAR(10) 学生名称
major VARCHAR(50) 专业
图书表:book
字段名称 数据类型 说明
BID CHAR(10) 图书编号,主键
title CHAR(50) 书名
author CHAR(20) 作者
借书信息表:borrow
字段名称 数据类型 说明
borrowID CHAR(10) 借书编号,主键
stuID CHAR(10) 学生编号,外键
BID CHAR(10) 图书编号,外键
T_time DATETIME 借书日期
B_time DATETIME 还书日期
CREATE DATABASE book
USE book
CREATE TABLE student(
stuID CHAR(10) PRIMARY KEY,
stuName VARCHAR(10),
major VARCHAR(50)
)
CREATE TABLE book(
bid CHAR(10),
title CHAR(10),
author CHAR(10)
)
CREATE TABLE borrow(
borrowID CHAR(10) PRIMARY KEY,
stuId CHAR(10),
BID CHAR(10),
T_time DATETIME,
B_time DATETIME
)
INSERT INTO student(stuID,stuName,major)VALUES(‘1001’,‘林林’,‘计算机’);
INSERT INTO student(stuID,stuName,major)VALUES(‘1002’,‘白杨’,‘计算机’);
INSERT INTO student(stuID,stuName,major)VALUES(‘1003’,‘虎子’,‘英语’);
INSERT INTO student(stuID,stuName,major)VALUES(‘1004’,‘北漂的雪’,‘工商管理’);
INSERT INTO student(stuID,stuName,major)VALUES(‘1005’,‘五月’,‘数学’);
–图书信息表中插入数据–
INSERT INTO book(BID,title,author)VALUES(‘B001’,‘人生若只如初见’,‘安意如’);
INSERT INTO book(BID,title,author)VALUES(‘B002’,‘入学那天遇见你’,‘晴空’);
INSERT INTO book(BID,title,author)VALUES(‘B003’,‘感谢折磨你的人’,‘如娜’);
INSERT INTO book(BID,title,author)VALUES(‘B004’,‘我不是教你诈’,‘刘庸’);
INSERT INTO book(BID,title,author)VALUES(‘B005’,‘英语四级’,‘白雪’);
–借书信息表中插入数据–
INSERT INTO borrow(borrowID,stuID,BID,T_time,B_time)VALUES(‘T001’,‘1001’,‘B001’,‘2007-12-26’,NULL);
INSERT INTO borrow(borrowID,stuID,BID,T_time,B_time)VALUES(‘T002’,‘1004’,‘B003’,‘2008-1-5’,NULL);
INSERT INTO borrow(borrowID,stuID,BID,T_time,B_time)VALUES(‘T003’,‘1005’,‘B001’,‘2007-10-8’,‘2007-12-25’);
INSERT INTO borrow(borrowID,stuID,BID,T_time,B_time)VALUES(‘T004’,‘1005’,‘B002’,‘2007-12-16’,‘2008-1-7’);
INSERT INTO borrow(borrowID,stuID,BID,T_time,B_time)VALUES(‘T005’,‘1002’,‘B004’,‘2007-12-22’,NULL);
INSERT INTO borrow(borrowID,stuID,BID,T_time,B_time)VALUES(‘T006’,‘1005’,‘B005’,‘2008-1-6’,NULL);
INSERT INTO borrow(borrowID,stuID,BID,T_time,B_time)VALUES(‘T007’,‘1002’,‘B001’,‘2007-9-11’,NULL);
INSERT INTO borrow(borrowID,stuID,BID,T_time,B_time)VALUES(‘T008’,‘1005’,‘B004’,‘2007-12-10’,NULL);
INSERT INTO borrow(borrowID,stuID,BID,T_time,B_time)VALUES(‘T009’,‘1004’,‘B005’,‘2007-10-16’,‘2007-12-18’);
INSERT INTO borrow(borrowID,stuID,BID,T_time,B_time)VALUES(‘T010’,‘1002’,‘B002’,‘2007-9-15’,‘2008-1-5’);
INSERT INTO borrow(borrowID,stuID,BID,T_time,B_time)VALUES(‘T011’,‘1004’,‘B003’,‘2007-12-28’,NULL);
INSERT INTO borrow(borrowID,stuID,BID,T_time,B_time)VALUES(‘T012’,‘1002’,‘B003’,‘2007-12-30’,NULL);
/* 1)显示出“计算机”专业学生在“2007-12-15”至“2008-1-8”时间段内借书的
学生编号、学生名称、图书编号、图书名称、借出日期;参考查询结果如下图所示:*/
SELECT *
FROM student
AS s,(SELECT * FROM borrow
) AS br,(SELECT * FROM book
) AS b
WHERE s.stuID
=br.stuId
AND b.bid
=br.bid AND T_time
BETWEEN ‘2007-12-15’ AND ‘2008-1-8’
2)查询所有借过图书的学生编号、学生名称、专业;参考查询结果如下图所示:
SELECT s.stuID
,s.major
,s.stuName
FROM student
AS s
WHERE s.stuID
IN (
SELECT stuID
FROM borrow
)
SELECT stuName
,s2.count1
FROM student
AS s,(SELECT stuID
,COUNT(stuId
) AS count1 FROM borrow
WHERE NOT T_time
IS NULL AND B_time
IS NULL GROUP BY stuID) AS s2
WHERE s2.stuID
=s.stuID