數據庫的子查詢

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操作符中

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章