SQL 子查詢和表連線

2021-10-24 16:58:46 字數 638 閱讀 3090

一條搜尋語句他的過濾條件**於另乙個搜尋語句的select結果

select cust_name,cust_contact  from customers  

where cust_id in

(select cust_id

from orders

where order_num in

(select order_num

from orderitems

where prod_id =

'tnt2'))

;

注意:子查詢時對於不同表的字段一定要限定是哪個表中的(通過表名.欄位名)

select cust_id,cust_name,

(select

count(*

)from orders

where orders.cust_id = customers.cust_id)

as orders)

from customers order

by cust_name;

子查詢和連線表

有如下兩張表 部門表和教師表 1 查詢出招生部門所有男老師姓名 子查詢放在where語句中 select tname,deptno from teacher where gender 男 and deptno in select deptno from dept where dname 招生部 se...

sql連線查詢,子查詢和分頁查詢

內連線查詢,有效的去除笛卡爾積,分兩種 根據所使用的比較方式不同,內連線分為等值連線 自然連線和自連線三種 舉乙個自連線例子 select e.name,m.name from employees e join employees m on e.id m.id 分為左外連線和右外連線,用於查詢乙個表...

sql的子連線查詢

一直沒有注重於sql這一塊,現在的專案有大量的統計,報表,以前都是多表連線或者用mybatis的延遲載入之類的,這幾天心血來潮自己查了一下,發現了sql子查詢這個好東西,感覺方便多了,速度也快多了 自我感覺 of course,以後還是要多用函式,好一點。sql的子查詢是可以在把資料查出來後在查出一...