Mysql學習 第十一章 子查詢

2021-10-23 21:51:20 字數 1193 閱讀 7026

子查詢(subquery),即巢狀在其他查詢中的查詢。

假如需要列出訂購物品 rgan01 的所有顧客的查詢步驟

檢索包含物品 rgan01 的所有訂單的編號。

檢索具有前一步驟列出的訂單編號的所有顧客的 id。

檢索前一步驟返回的所有顧客 id的顧客資訊。

使用select步驟

select order_num

from orderitems

where prod_id = 'rgan01';

#檢索物品'rgan01'的訂單編號

select cust_id

from orders

where order_num in (20007,20008);

#用剛才檢索出來的編號查詢使用者id

結合這兩個查詢,把第乙個查詢(返回訂單號的那乙個)變為子查詢。

select cust_id

from orders

where order_num in (select order_num

from orderitems

where prod_id = 'rgan01');

。假如需要顯示 customers 表中每個顧客的訂單總數。訂單與相應的顧客 id儲存在 orders 表中。

從 customers 表中檢索顧客列表;

對於檢索出的每個顧客,統計其在 orders 表中的訂單數目。

select distinct cust_name, cust_state, (select count(*)

from orders

where orders.cust_id = customers.cust_id) as orders

from customers

order by cust_name;

子查詢中的 where 子句與前面使用的 where 子句稍有不同,因為它使用了完全限定列名,而不只是列名( cust_id )。它指定表名和列名( orders.cust_id 和 customers.cust_id )。下面的 where 子句告訴 sql,比較 orders 表中的 cust_id 和當前正從 customers 表中檢索的 cust_id :where orders.cust_id = customers.cust_id

modern c design 第十一章

本章介紹了經常遇到的雙分派的一種泛型解決方案。c 在語法上實現了單分派,即虛函式,通過動態機制選擇相應的函式。雙分派是形如fun object1 a,object2 b 根據a和b的實際型別動態自動分派乙個處理函式。最容易想到的方案,蠻幹法 寫一大堆過載函式.不過這種方法會有很強的依賴性。也提供了一...

第十一章3

第十一章 一 滾動元件 awt中的滾動元件包括scrollbar 滾動條 和滾動面板 scrollpane 兩種。1 滾動條scrollbar 在指定的取值範圍內快速選取某一值的功能。i.構造方法 public scrollbar int orientation,int value,int visi...

java第十一章

問題 1 錯誤 2 異常,1 編譯時異常 檢查異常 2 執行時異常 不檢查異常 是否能用 解決,是的就是異常,不是的就是錯誤 問題 1 先驗 2 捕獲 異常捕獲機智 異常的三個種類 1 檢查異常 2 執行時異常 3 錯誤 throw 提示方法呼叫者本方法可能發生異常 throw跟異常物件 throw...