子查询

Posted 浅色夏沫

tags:

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

1、子查询是指在另一个查询语句中的SELECT子句。

例句:

SELECT * FROM t1 WHERE column1 = (SELECT column1 FROM t2);

其中,SELECT * FROM t1 ...称为Outer Query[外查询](或者Outer Statement),

         SELECT column1 FROM t2 称为Sub Query[子查询]。

所以,子查询是指嵌套在查询内部,且必须始终出现在圆括号内。而事实上它有可能在子查询内部再嵌套子查询。

子查询可以包含多个关键字或条件,如DISTINCT,GROUP BY,ORDER BY, LIMIT,函数等。
子查询的外层查询可以是:SELECT,INSERT,UPDATE,SET或DO.

子查询可以返回标量、一行、一列或子查询。

2、使用比较运算符的子查询

=、>, <, >=, <=, <>, !=, <=>

operand comparison_operator subquery

mysql> SELECT AVG(goods_price) FROM tdb_goods; # 求平均值
+------------------+
| AVG(goods_price) |
+------------------+
| 5636.3636364 |
+------------------+
1 row in set (0.01 sec)

mysql> SELECT ROUND(AVG(goods_price), 2) FROM tdb_goods; ## 求平均值,并保留两位小数
+----------------------------+
| ROUND(AVG(goods_price), 2) |
+----------------------------+
| 5636.36 |
+----------------------------+
1 row in set (0.02 sec)

mysql> SELECT goods_id,goods_name,goods_price FROM tdb_goods WHERE goods_price
>= 5636.36;  #查询
+----------+----------------------------------+-------------+
| goods_id | goods_name | goods_price |
+----------+----------------------------------+-------------+
| 3 | G150TH 15.6英寸游戏本 | 8499.000 |
| 7 | SVP13226SCB 13.3英寸触控超极本 | 7999.000 |
| 13 | iMac ME086CH/A 21.5英寸一体电脑 | 9188.000 |
| 17 | Mac Pro MD878CH/A 专业级台式电脑 | 28888.000 |
| 18 | HMZ-T3W 头戴显示设备 | 6999.000 |
| 20 | X3250 M4机架式服务器 2583i14 | 6888.000 |
| 21 | HMZ-T3W 头戴显示设备 | 6999.000 |
+----------+----------------------------------+-------------+

mysql> SELECT goods_id,goods_name,goods_price FROM tdb_goods WHERE goods_price
>= (SELECT ROUND(AVG(GOODS_PRICE), 2) FROM tdb_goods); #通过子查询实现
+----------+----------------------------------+-------------+
| goods_id | goods_name | goods_price |
+----------+----------------------------------+-------------+
| 3 | G150TH 15.6英寸游戏本 | 8499.000 |
| 7 | SVP13226SCB 13.3英寸触控超极本 | 7999.000 |
| 13 | iMac ME086CH/A 21.5英寸一体电脑 | 9188.000 |
| 17 | Mac Pro MD878CH/A 专业级台式电脑 | 28888.000 |
| 18 | HMZ-T3W 头戴显示设备 | 6999.000 |
| 20 | X3250 M4机架式服务器 2583i14 | 6888.000 |
| 21 | HMZ-T3W 头戴显示设备 | 6999.000 |
+----------+----------------------------------+-------------+

mysql> SELECT goods_price FROM tdb_goods WHERE goods_cate = \'超级本\';
+-------------+
| goods_price |
+-------------+
| 4999.000 |
| 4299.000 |
| 7999.000 |
+-------------+
3 rows in set (0.00 sec)

mysql> SELECT * FROM tdb_goods WHERE goods_cate = \'超级本\'\\G;
*************************** 1. row ***************************
goods_id: 5
goods_name: X240(20ALA0EYCD) 12.5英寸超极本
goods_cate: 超级本
brand_name: 联想
goods_price: 4999.000
is_show: 1
is_saleoff: 0
*************************** 2. row ***************************
goods_id: 6
goods_name: U330P 13.3英寸超极本
goods_cate: 超级本
brand_name: 联想
goods_price: 4299.000
is_show: 1
is_saleoff: 0
*************************** 3. row ***************************
goods_id: 7
goods_name: SVP13226SCB 13.3英寸触控超极本
goods_cate: 超级本
brand_name: 索尼
goods_price: 7999.000
is_show: 1
is_saleoff: 0

mysql> SELECT goods_price FROM tdb_goods WHERE goods_cate = \'超级本\';
+-------------+
| goods_price |
+-------------+
| 4999.000 |
| 4299.000 |
| 7999.000 |
+-------------+

mysql> SELECT goods_id,goods_name,goods_price FROM tdb_goods WHERE goods_price >
(SELECT goods_price FROM tdb_goods WHERE goods_cate = \'超极本\');
Empty set (0.00 sec)

 

mysql> SELECT goods_id,goods_name,goods_price FROM tdb_goods WHERE goods_price >

ANY (SELECT goods_price FROM tdb_goods WHERE goods_cate = \'超极本\');
Empty set (0.00 sec)  
#大于any,大于其中任何一个都可以

 

 

 

3、使用[NOT] IN 的子查询

ANY运算符与IN等效,

!=ALL或<>ALL运算符与NOT IN 等效

 

mysql> SELECT goods_id,goods_name,goods_price FROM tdb_goods WHERE goods_price !
= ALL (SELECT goods_price FROM tdb_goods WHERE goods_cate = \'超极本\');

 

其余19种记录

4、使用[NOT] EXISTS 的子查询(很少用)

如果子查询返回任何行,EXISTS将返回TRUE,否则返回FALSE.

 

以上是关于子查询的主要内容,如果未能解决你的问题,请参考以下文章

MySQL子查询(六)

SQl子查询

子查询分解问题

相关子查询 和 不相关子查询

SQL Server 之 子查询与嵌套查询

MySQL—— 子查询