码迷,mamicode.com
首页 > 其他好文 > 详细

子查询

时间:2017-03-26 16:17:11      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:ima   row   实现   image   group   索尼   exists   mysql   color   

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.

 

子查询

标签:ima   row   实现   image   group   索尼   exists   mysql   color   

原文地址:http://www.cnblogs.com/toudoubao/p/6623009.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!