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测试

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

扫描二维码关注公众号,回复: 114689 查看本文章

  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;
将查询结果写入到数据表测试

猜你喜欢

转载自www.cnblogs.com/darwinli/p/8997014.html
今日推荐