1、子查詢:
使用子查詢是指,在一個select語句中還嵌套着另一個select語句
示例:
select cust_id
from orders
where order_num in
(select order_num
from orderItems
where prod_id = 'RGAN01');
注意:
作爲子查詢的select語句只能查詢單個列,企圖檢索多個咧將返回錯誤
2、作爲計算字段使用子查詢
我們可以將子查詢獲得的值作爲計算字段,示例:
select cust_name, cust_state ,
(select count(*)
from orders
where orders.cust_id = customers.cust_id)
as orders
from custoomers
order by cust_name;
注意:
子查詢最常見的使用是在where子句的in操作符中