• 子查询:指一个“正常查询语句”中的某个部分(比如select部分,from 部分,where 部分)又出现了查询的一种查询形式,比如:
select * from 表名 where age > (一个子查询语句);
select * from 表名 where age > (select avg(age) from 表名);

可以根据需求不断的嵌套子查询
select * from 表名 where 字段名 > (select avg(字段名) from 表名 where 字段名 < (select ...));
  • 测试数据
mysql> select * from test;
+----+--------+------+------+
| id | name   | sex  | age  |
+----+--------+------+------+
|  1 | name1  | 女   |   15 |
|  2 | name1  | 女   |   15 |
|  4 | name2  | 男   |   30 |
|  5 | name50 | 男   |   12 |
+----+--------+------+------+
查询出大于平均年龄的用户数据
mysql> select * from test where age > (select avg(age) from test);
+----+-------+------+------+
| id | name  | sex  | age  |
+----+-------+------+------+
|  4 | name2 | 男   |   30 |
+----+-------+------+------+


  • 标量子查询


含义: |
标量子查询就是指子查询的结果是“单个值”(一行一列)的查询。

使用:
标量子查询通常用在where子句中,作为主查询的一个条件判断的数据。
本质上,标量子查询的结果,就可以直接当做“一个值”来使用。
取出的平均值除去标题栏,就是一行一列,查询结果可以直接当做值来使用
mysql> select avg(age) from test;
+----------+
| avg(age) |
+----------+
|  18.0000 |
+----------+

类似于这样的查询也是一行一列,可以当做值来使用
mysql> select 18 as age;
+-----+
| age |
+-----+
|  18 |
+-----+

上面的两种写法都可以代替这个 18 数字,区别就是一个是固定写死的,一个是通过查询计算之后得到的值,非固定值。
mysql> select * from test where age > 18;
+----+-------+------+------+
| id | name  | sex  | age  |
+----+-------+------+------+
|  4 | name2 | 男   |   30 |
+----+-------+------+------+

查询结果都是一样的
mysql> select * from test where age > (select 18);
+----+-------+------+------+
| id | name  | sex  | age  |
+----+-------+------+------+
|  4 | name2 | 男   |   30 |
+----+-------+------+------+

查询结果都是一样的
mysql> select * from test where age > (select avg(age) from test);
+----+-------+------+------+
| id | name  | sex  | age  |
+----+-------+------+------+
|  4 | name2 | 男   |   30 |
+----+-------+------+------+


  • 列子查询


含义:
列子查询查出的结果为“一列数据”,类似这样:

mysql> select name from user;
+-------+
| name  |
+-------+
| name1 |
| name2 |
| name3 |
| name4 |
+-------+

使用;
列子查询通常用在 where 子句的 in 运算符中,代替 in 运算符中的“字面值”列表数据。

比如:
select * from product where chandi in ('北京', '深圳', '天津')
如果 in 中的数据并不方便一个一个列出,但可以通过一个查询得到,就可以使用查询来实现:
select * from product where chandi in (select chandi from product);
相当于就是 in 中的数据无法固定写死,需要通过动态的去查询出来一个列表放到 in 中去动态使用。


  • 行子查询


含义:
行子查询查出的结果通常是一行,类似这样:

mysql> select name,age from user where age > 15;
+-------+------+
| name  | age  |
+-------+------+
| name1 |   18 |
| name3 |   20 |
| name4 |   30 |
+-------+------+

使用:
行子查询的结果通常跟“行构造符”一起,在 where 条件子句中做为条件数据,类似这样:
select * from 表名 where (字段 1, 字段2) = (行子查询)
或这样,含义跟上一行是一样的, 即 row 可以省略
select * from 表名 where row(字段1, 字段2) = (行子查询)

使用场景举例:找出指定省市的所有用户,这样就可以这么写查询了
select * from 表名 where row(省字段名, 市字段名) = ('上海', '徐汇区');


  • 查询


含义:
当一个子查询查出的结果是“多行多列”的时候,就是查询。
查询的结果相当于一个表,可以直接当做一个表来使用。

使用:
查询通常用在主查询的 from 子句中,作为一个“数据源”。

通常我们查询表是这样:
select * from 表名 where 字段名 > 0;
但是现在这个表就不固定了,需要我们通过查询语句来得到并使用,这个查询的表必须要起别名:
select * from (select * from 表名 where 字段名 > 0) as table1;

注意:
此时需要给该子查询设置一个别名,必须要加别名!!!,类似这样:
select * from (select... 子查询) as table1;
查询出年龄 age > 12 的用户
mysql> select * from (select * from test where age > 12) as table1;
+----+-------+------+------+
| id | name  | sex  | age  |
+----+-------+------+------+
|  1 | name1 | 女   |   15 |
|  2 | name1 | 女   |   15 |
|  4 | name2 | 男   |   30 |
+----+-------+------+------+

查询出年龄 age > 12 的用户总人数与平均年龄
mysql> select count(*) as 总人数, avg(age) as 平均年龄 from (select * from test where age > 12) as table1;
+-----------+--------------+
| 总人数    | 平均年龄     |
+-----------+--------------+
|         3 |      20.0000 |
+-----------+--------------+


  • 子查询的特定关键字


  • any、some、in、all、exists 关键字