当前位置:   article > 正文

MySQL 嵌套查询

MySQL 嵌套查询

嵌套查询

是指在一个完整的查询语句之中,包含若干个不同功能的小查询;从而一起完成复杂查询的一种编写形式。包含的查询放在()里 , 包含的查询出现的位置:

位置含义
SELECT之后把查询结果作为表头使用
FROM之后把查询结果作为表使用
WHERE之后把查询结果作为查询条件
HAVING之后把查询结果作为过滤使用

SELECT之后:

查看客户的总订单数

  1. mysql> select cust_name,
  2. -> (select count(*) from orders
  3. -> where orders.cust_id = customers.cust_id )
  4. -> as orders_num from customers;
  5. +----------------+------------+
  6. | cust_name | orders_num |
  7. +----------------+------------+
  8. | Coyote Inc. | 2 |
  9. | Mouse House | 0 |
  10. | Wascals | 1 |
  11. | Yosemite Place | 1 |
  12. | E Fudd | 1 |
  13. +----------------+------------+
  14. 5 rows in set (0.01 sec)

WHERE之后:

查询下单了TNT2的客户id

  1. mysql> select cust_id from orders
  2. -> where order_num in
  3. -> ( select order_num from orderitems
  4. -> where prod_id = 'TNT2' );
  5. +---------+
  6. | cust_id |
  7. +---------+
  8. | 10001 |
  9. | 10004 |
  10. +---------+
  11. 2 rows in set (0.01 sec)

实验:

1.使用子查询,返回购买价格为10或以上的商品的客户列表。您需,然后使用要使用Orderltems表查找匹配的订单号(order num)Orders表检索每个匹配订单的客户ID(cust id)。

  1. mysql> select distinct cust_id from orders
  2. -> where order_num in
  3. -> ( select order_num from orderitems
  4. -> where item_price >= 10 ) ;
  5. +---------+
  6. | cust_id |
  7. +---------+
  8. | 10001 |
  9. | 10003 |
  10. | 10004 |
  11. +---------+
  12. 3 rows in set (0.01 sec)

2.您需要知道订购产品BR01的日期。编写一条SQL语句,使用子查询确定哪些订单(Orderltems中)购买了prod_id为BR01的商品,然后返回客户ID(cust_id),和订单日期(order_date)。按订单日期排序结果。

  1. mysql> select order_date from orders
  2. -> where order_num in
  3. -> ( select order_num from orderitems
  4. -> where prod_id = 'ANV01');
  5. +---------------------+
  6. | order_date |
  7. +---------------------+
  8. | 2023-09-01 00:00:00 |
  9. +---------------------+
  10. 1 row in set (0.00 sec)

3.更新前面的挑战,为购买了prod id为AVN01的商品的任何客户返回客户电子邮件(Customers表中的custemail)。这里有一个提示:这涉及到SELECT语句,最里面的查询从Orderltems返回order num,中间的查询从Customers返回custid。

  1. mysql> select cust_email from customers
  2. -> where cust_id in
  3. -> ( select cust_id from orders
  4. -> where order_num in
  5. -> ( select order_num from orderitems
  6. -> where prod_id = 'ANV01' ) );
  7. +-----------------+
  8. | cust_email |
  9. +-----------------+
  10. | ylee@coyote.com |
  11. +-----------------+
  12. 1 row in set (0.00 sec)

4.您需要一个包含每个客户订购的总额的客户ID列表。编写一条SOL语句,返回客户ID(0rders表中的cust id)和total ordered,并使用一个子查询返回每个客户的订单总数。按花费从大到小的顺序排列结果。这里有一个提示:你已经使用SUM()来计算订单总计。

  1. mysql> SELECT cust_id,
  2. -> ( SELECT SUM(quantity * item_price ) FROM orderitems
  3. -> WHERE order_num IN
  4. -> ( SELECT order_num FROM orders
  5. -> WHERE orders.cust_id = customers.cust_id ) )
  6. -> AS total_ordered
  7. -> FROM customers
  8. -> ORDER BY total_ordered DESC;
  9. +---------+---------------+
  10. | cust_id | total_ordered |
  11. +---------+---------------+
  12. | 10004 | 1000.00 |
  13. | 10001 | 188.34 |
  14. | 10005 | 125.00 |
  15. | 10003 | 55.00 |
  16. | 10002 | NULL |
  17. +---------+---------------+
  18. 5 rows in set (0.00 sec)

5.编写一条SQL语句,从Products表中检索所有产品名称(prodname),以及一个名为quant_sold的计算列,该列包含此商品的销售总数(使用Orderltems表中的子查询和SUM(quantity)检索)。

  1. mysql> SELECT prod_name,
  2. -> (SELECT Sum(quantity)
  3. -> FROM orderitems
  4. -> WHERE products.prod_id=orderitems.prod_id)
  5. -> AS quant_sold
  6. -> FROM products;
  7. +----------------+------------+
  8. | prod_name | quant_sold |
  9. +----------------+------------+
  10. | .5 ton anvil | 10 |
  11. | 1 ton anvil | 3 |
  12. | 2 ton anvil | 1 |
  13. | Detonator | NULL |
  14. | Bird seed | 2 |
  15. | Carrots | 50 |
  16. | Fuses | NULL |
  17. | JetPack 1000 | NULL |
  18. | JetPack 2000 | 1 |
  19. | Oil can | 1 |
  20. | Safe | NULL |
  21. | Sling | 1 |
  22. | TNT (1 stick) | NULL |
  23. | TNT (5 sticks) | 105 |
  24. +----------------+------------+
  25. 14 rows in set (0.00 sec)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/404727
推荐阅读
相关标签
  

闽ICP备14008679号