MySQL学习日志十,子查询和嵌套查询

Posted yzx-sir

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL学习日志十,子查询和嵌套查询相关的知识,希望对你有一定的参考价值。

子查询

where(这个值是计算出来的)
先执行where里面的语句,之后执行外面的语句

本质:where里面嵌套一个子查询语句

查询参加高等数学考试的学生(学号,姓名。科目编号,成绩),降序排列

-- 方式一:连接查询

SELECT s.studentno,studentname,r.subjectno,studentresult
FROM student s
INNER JOIN result r
on s.studentno=r.studentno
INNER JOIN `subject`sub
on sub.subjectno=r.subjectno
WHERE subjectname=\'高等数学-1\'
order by studentresult DESC

-- 方式二:子查询

SELECT s.studentno,studentname,r.subjectno,studentresult
FROM student s
INNER JOIN result r
on s.studentno=r.studentno
WHERE subjectno=(
SELECT subjectno from `subject` WHERE subjectname=\'高等数学-1\'
)
order by studentresult DESC

-- 查询分数不小于80的学生的学号,姓名

SELECT s.studentno,studentname
from student s
INNER JOIN result r
on s.studentno=r.studentno
WHERE studentresult>=80

-- 查询参加高等数学-1分数不小于80的学生的学号,姓名

SELECT s.studentno,studentname
from student s
INNER JOIN result r
on s.studentno=r.studentno
WHERE studentresult>=80
 and 
subjectno=(
SELECT subjectno from `subject` WHERE subjectname=\'高等数学-1\'
)

-- 查询参加高等数学-1分数不小于80的学生的学号,姓名
-- 嵌套查询(由里及外)

SELECT studentno,studentname
from student
WHERE studentno in(
SELECT studentno FROM result WHERE studentresult>=80
 and 
subjectno=(
SELECT subjectno from `subject` WHERE subjectname=\'高等数学-1\'
))

-- 查询参加高等数学-1,前5名同学的信息(学号,姓名,成绩)
-- 联表查询

SELECT s.studentno,studentname,studentresult
from student s
INNER JOIN result r
on s.studentno=r.studentno
INNER JOIN `subject` sub
ON r.subjectno=sub.subjectno
WHERE subjectname=\'高等数学-1\'
ORDER BY studentresult DESC
LIMIT 0,5

-- 查询参加高等数学-1,前5名同学的信息(学号,姓名,成绩)
-- 子查询

SELECT s.studentno,studentname,studentresult
from student s
INNER JOIN result r
on s.studentno=r.studentno
WHERE subjectno= (
SELECT subjectno from `subject` WHERE subjectname=\'高等数学-1\'
)
ORDER BY studentresult DESC
LIMIT 0,5

MYSQL初级学习笔记六:子查询!(视频序号:初级_37-41)

知识点八:子查询(42)

什么是子查询:

  子查询是将一个查询语句嵌套在另一个查询语句中。内层查询语句的查询结果,可以作为外层查询语句提供条件。

引发子查询的情况:

  使用[NOT] IN 的子查询

技术分享图片
 1 --删除原本的员工表和部门表,重新建
 2 DROP TABLE employee,department;
 3 
 4 CREATE TABLE IF NOT EXISTS department(
 5 id TINYINT UNSIGNED AUTO_INCREMENT KEY,
 6 depName VARCHAR(20) NOT NULL UNIQUE
 7 )ENGINE=INNODB;
 8 
 9 INSERT department(depName) VALUES(教学部),
10 (市场部),
11 (运营部),
12 (督导部);
13 
14 -- 创建员工表employee(子表)
15 -- id ,username ,depId
16 CREATE TABLE IF NOT EXISTS employee(
17 id SMALLINT UNSIGNED AUTO_INCREMENT KEY,
18 username VARCHAR(20) NOT NULL UNIQUE,
19 depId TINYINT UNSIGNED
20 )ENGINE=INNODB;
21 
22 INSERT employee(username,depId) VALUES(king,1),
23 (queen,2),
24 (张三,3),
25 (李四,4),
26 (王五,1);
27 
28 -- 由[NOT] IN引发的子查询
29 SELECT id FROM department;
30 
31 SELECT id,username FROM employee WHERE depId IN(1,2,3,4);
32 
33 SELECT id,username FROM employee WHERE depId IN(SELECT id FROM department);
34 
35 SELECT id,username FROM employee WHERE depId NOT IN(SELECT id FROM department);
36 
37 INSERT employee(username,depId) VALUES(testtest,8);
测试[NOT IN]子查询

  使用比较运算符的子查询:

    =>,<,>=,<=,<>,!=,ó

  使用[NOT] EXISTS的子查询:

技术分享图片
 1 -- 创建学员表student
 2 -- id username score
 3 CREATE TABLE IF NOT EXISTS student(
 4 id TINYINT UNSIGNED AUTO_INCREMENT KEY,
 5 username VARCHAR(20)  NOT NULL UNIQUE,
 6 score TINYINT UNSIGNED
 7 );
 8 INSERT student(username,score) VALUES(king,95),
 9 (king1,35),
10 (king2,45),
11 (king3,55),
12 (king4,65),
13 (king5,75),
14 (king6,80),
15 (king7,90),
16 (king8,25);
17 -- 创建奖学金scholarship
18 -- id ,level
19 
20 CREATE TABLE IF NOT EXISTS scholarship(
21 id TINYINT UNSIGNED AUTO_INCREMENT KEY,
22 level TINYINT UNSIGNED
23 );
24 INSERT scholarship(level) VALUES(90),(80),(70);
25 
26 
27 -- 查询获得1等奖学金的学员有
28 
29 SELECT level FROM scholarship WHERE id=1;
30 
31 SELECT id,username FROM student WHERE score>=90;
32 
33 SELECT id,username FROM student WHERE score>=(SELECT level FROM scholarship WHERE id=1);
34 
35 -- 查询部门表中
36 
37 SELECT * FROM department WHERE id=5;
38 
39 SELECT id,username FROM employee WHERE EXISTS(SELECT * FROM department WHERE id=5);
40 
41 SELECT id,username FROM employee WHERE EXISTS(SELECT * FROM department WHERE id=4);
42 
43 SELECT id,username FROM employee WHERE NOT EXISTS(SELECT * FROM department WHERE id=41);
比较运算符与[NOT] EXISTS测试

  使用ANY | SOME 或者ALL的子查询:

运算符                关键字 ANY SOME ALL
>>= 最小值 最小值 最大值
<<= 最大值 最大值 最小值
= 任意值 任意值  
<>!=     任意值
技术分享图片
 1 -- 查询所有获得奖学金的学员
 2 
 3 SELECT id,username,score FROM student WHERE score>=ANY(SELECT level FROM scholarship);
 4 
 5 
 6 SELECT id,username,score FROM student WHERE score>=SOME(SELECT level FROM scholarship);
 7 
 8 -- 查询所有学员中获得一等奖学金的学员
 9 SELECT id,username,score FROM student WHERE score >=ALL(SELECT level FROM scholarship);
10 
11 -- 查询学员表中没有获得奖学金的学员
12 
13 SELECT id,username,score FROM student WHERE score<ALL(SELECT level FROM scholarship);
14 
15 
16 SELECT id,username,score FROM student WHERE score<ANY(SELECT level FROM scholarship);
17 
18 SELECT id,username,score FROM student WHERE score<=ANY(SELECT level FROM scholarship);
19 
20 -- 相当于IN
21 SELECT id,username,score FROM student WHERE score=ANY(SELECT level FROM scholarship);
22 
23 SELECT id,username,score FROM student WHERE score IN(SELECT level FROM scholarship);
24 
25 -- 相当于NOT IN
26 SELECT id,username,score FROM student WHERE score NOT IN(SELECT level FROM scholarship);
27 
28 SELECT id,username,score FROM student WHERE score <> ALL(SELECT level FROM scholarship);
ANY,SOME,ALL测试

 

将查询结果写入到数据表:

  INSERT [INTO] tbl_name [(col_name,…)] SELECT …

建数据表同时将查询结果写入到数据表:

  CREATE TABLE [IF NOT EXISTS] tbl_name

  [(create_definition,…)]

  select_statement

技术分享图片
 1 --将查询结果写入到数据表
 2 CREATE TABLE test1 (
 3 id TINYINT UNSIGNED AUTO_INCREMENT KEY,
 4 num TINYINT UNSIGNED
 5 );
 6 INSERT test1(id,num) 
 7 SELECT id,score FROM student;
 8 
 9 
10 CREATE TABLE test2 (
11 id TINYINT UNSIGNED AUTO_INCREMENT KEY,
12 num TINYINT UNSIGNED
13 )SELECT id,score FROM student;
14 
15 CREATE TABLE test3 (
16 id TINYINT UNSIGNED AUTO_INCREMENT KEY,
17 score TINYINT UNSIGNED
18 )SELECT id,score FROM student;
将查询结果写入到数据表测试

 

以上是关于MySQL学习日志十,子查询和嵌套查询的主要内容,如果未能解决你的问题,请参考以下文章

吐血整理mysql子查询多重嵌套

MYSQL初级学习笔记六:子查询!(视频序号:初级_37-41)

MySQL子查询(六)

嵌套套娃,MySQL子查询,单行与多行子查询,相关和不相关(关联)子查询,完整详细可收藏

MySQL学习笔记—— 子查询及联结

MYSQL学习笔记之子查询