MySQL中的嵌套查詢
嵌套查詢
是指在一個(gè)完整的查詢語句之中,包含若干個(gè)不同功能的小查詢;從而一起完成復(fù)雜查詢的一種編寫形式。
包含的查詢放在()里 , 包含的查詢出現(xiàn)的位置:
位置 | 含義 |
SELECT之后 | 把查詢結(jié)果作為表頭使用 |
FROM之后 | 把查詢結(jié)果作為表使用 |
WHERE之后 | 把查詢結(jié)果作為查詢條件 |
HAVING之后 | 把查詢結(jié)果作為過濾使用 |
SELECT之后
查看客戶的總訂單數(shù)
mysql> select cust_name, -> (select count(*) from orders -> where orders.cust_id = customers.cust_id ) -> as orders_num from customers; +----------------+------------+ | cust_name | orders_num | +----------------+------------+ | Coyote Inc. | 2 | | Mouse House | 0 | | Wascals | 1 | | Yosemite Place | 1 | | E Fudd | 1 | +----------------+------------+ 5 rows in set (0.01 sec)
WHERE之后
查詢下單了TNT2的客戶id
mysql> select cust_id from orders -> where order_num in -> ( select order_num from orderitems -> where prod_id = 'TNT2' ); +---------+ | cust_id | +---------+ | 10001 | | 10004 | +---------+ 2 rows in set (0.01 sec)
實(shí)驗(yàn)
1.使用子查詢,返回購買價(jià)格為10或以上的商品的客戶列表。您需,然后使用要使用Orderltems表查找匹配的訂單號(hào)(order num)Orders表檢索每個(gè)匹配訂單的客戶ID(cust id)。
mysql> select distinct cust_id from orders -> where order_num in -> ( select order_num from orderitems -> where item_price >= 10 ) ; +---------+ | cust_id | +---------+ | 10001 | | 10003 | | 10004 | +---------+ 3 rows in set (0.01 sec)
2.您需要知道訂購產(chǎn)品BR01的日期。編寫一條SQL語句,使用子查詢確定哪些訂單(Orderltems中)購買了prod_id為BR01的商品,然后返回客戶ID(cust_id),和訂單日期(order_date)。按訂單日期排序結(jié)果。
mysql> select order_date from orders -> where order_num in -> ( select order_num from orderitems -> where prod_id = 'ANV01'); +---------------------+ | order_date | +---------------------+ | 2023-09-01 00:00:00 | +---------------------+ 1 row in set (0.00 sec)
3.更新前面的挑戰(zhàn),為購買了prod id為AVN01的商品的任何客戶返回客戶電子郵件(Customers表中的custemail)。這里有一個(gè)提示:這涉及到SELECT語句,最里面的查詢從Orderltems返回order num,中間的查詢從Customers返回custid。
mysql> select cust_email from customers -> where cust_id in -> ( select cust_id from orders -> where order_num in -> ( select order_num from orderitems -> where prod_id = 'ANV01' ) ); +-----------------+ | cust_email | +-----------------+ | ylee@coyote.com | +-----------------+ 1 row in set (0.00 sec)
4.您需要一個(gè)包含每個(gè)客戶訂購的總額的客戶ID列表。編寫一條SOL語句,返回客戶ID(0rders表中的cust id)和total ordered,并使用一個(gè)子查詢返回每個(gè)客戶的訂單總數(shù)。按花費(fèi)從大到小的順序排列結(jié)果。這里有一個(gè)提示:你已經(jīng)使用SUM()來計(jì)算訂單總計(jì)。
mysql> SELECT cust_id, -> ( SELECT SUM(quantity * item_price ) FROM orderitems -> WHERE order_num IN -> ( SELECT order_num FROM orders -> WHERE orders.cust_id = customers.cust_id ) ) -> AS total_ordered -> FROM customers -> ORDER BY total_ordered DESC; +---------+---------------+ | cust_id | total_ordered | +---------+---------------+ | 10004 | 1000.00 | | 10001 | 188.34 | | 10005 | 125.00 | | 10003 | 55.00 | | 10002 | NULL | +---------+---------------+ 5 rows in set (0.00 sec)
5.編寫一條SQL語句,從Products表中檢索所有產(chǎn)品名稱(prodname),以及一個(gè)名為quant_sold的計(jì)算列,該列包含此商品的銷售總數(shù)(使用Orderltems表中的子查詢和SUM(quantity)檢索)。
mysql> SELECT prod_name, -> (SELECT Sum(quantity) -> FROM orderitems -> WHERE products.prod_id=orderitems.prod_id) -> AS quant_sold -> FROM products; +----------------+------------+ | prod_name | quant_sold | +----------------+------------+ | .5 ton anvil | 10 | | 1 ton anvil | 3 | | 2 ton anvil | 1 | | Detonator | NULL | | Bird seed | 2 | | Carrots | 50 | | Fuses | NULL | | JetPack 1000 | NULL | | JetPack 2000 | 1 | | Oil can | 1 | | Safe | NULL | | Sling | 1 | | TNT (1 stick) | NULL | | TNT (5 sticks) | 105 | +----------------+------------+ 14 rows in set (0.00 sec)
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
一文總結(jié)MySQL中數(shù)學(xué)函數(shù)有哪些
MySQL函數(shù)包括數(shù)學(xué)函數(shù)、字符串函數(shù)、日期和時(shí)間函數(shù)、條件判斷函數(shù)、系統(tǒng)信息函數(shù)、加密函數(shù)等,下面這篇文章主要給大家介紹了關(guān)于MySQL中數(shù)學(xué)函數(shù)有哪些的相關(guān)資料,需要的朋友可以參考下2023-02-02MySQL允許遠(yuǎn)程登錄的操作實(shí)現(xiàn)
本文主要介紹了MySQL允許遠(yuǎn)程登錄的操作實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-02-02MySQL InnoDB架構(gòu)的相關(guān)總結(jié)
InnoDB存儲(chǔ)引擎架構(gòu)作為MySQL最常用的存儲(chǔ)引擎,每個(gè)后端程序員都應(yīng)有所了解,本文將具體講述MySQL InnoDB架構(gòu)的相關(guān)知識(shí),感興趣的朋友可以參考下2021-05-05MySQL 主從復(fù)制數(shù)據(jù)不一致的解決方法
本文主要介紹了MySQL 主從復(fù)制數(shù)據(jù)不一致的解決方法,文中根據(jù)實(shí)例編碼詳細(xì)介紹的十分詳盡,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03