SQL使用子查詢 聯結表 建立高階聯結

2021-09-25 08:35:30 字數 4708 閱讀 1579

利用子查詢進行過濾

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 = 'rgan01'));

在select語句中,子查詢總是向內而外處理。

上述語句檢索了訂購物品rgan01的所有顧客的資訊。

總共分為3步:

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

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

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

作為子查詢的select語句只能查詢單個列。企圖檢索多個列將返回錯誤。

作為計算字段使用子查詢

需要顯示customers表中每個顧客的訂單總數。

分為兩步:

a、從customers表中檢索顧客列表

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

select cust_name, 

cust_state,

(select count(*)

from orders

where orders.cust_id = customers.cust_id) as orders

from customers

order by cust_name;

聯結是一種機制,用來在一條select語句中關聯表。使用特殊的語法,可以聯結多個表返回一組輸出,聯結在執行時關聯表中正確的行。

建立聯結

select vend_name, prod_name, prod_price

from vendors, products

where vendors.vend_id = products.vend_id;

笛卡兒積

由沒有聯結條件的表關係返回的結果為笛卡兒積。檢索出的行的數目將是第乙個表中的行數乘以第二個表中的行數。

注:要保證所有聯結都有where子句,否則dbms將返回比想要的資料多得多的資料。同理,要保證where子句的正確性。不正確的過濾條件會導致dbms返回不正確的資料

有時,返回笛卡兒積的聯結,又稱為叉聯結(cross join)。

內聯結目前為止使用de聯結稱為等值聯結(equijoin),它基於兩個表之間的相等測試。這種聯結也稱為內聯結(inner join)。其實,可以明確指定聯結的型別。

select vend_name, prod_name, prod_price

from vendors inner join products

on vendors.vend_id = products.vend_id;

這裡,兩個表之間的關係是以inner join指定的部分from子句。在使用這種語法時,聯結條件用特定的on子句而不是where子句給出。

聯結多個表

dbms在執行時關聯指定的每個表。這種處理可能非常耗費資源。聯結的表越多,效能下降越厲害。

本文章開頭的例子中,由於子查詢並不總是執行複雜select操作的最有效辦法,下面是使用聯結的相同查詢

select cust_name, cust_contact

from customers, orders, orderitems

where customers.cust_id = orders.cust_id

and orders.order_num = orderitems.order_num

and orderitems.prod_id = 'rgan01';

使用表別名

sql除了可以對列名和計算字段使用別名,還允許給表名起別名。原因如下:

a、縮短sql語句

b、允許在一條select語句正則多次使用相同的表

select cust_name, cust_contact

from customers as c, orders as o, orderitems as oi

where c.cust_id = o.cust_id

and o.order_num = oi.order_num

and oi.prod_id = 'rgan01';

表別名不僅能用於where子句,還可以用於select的列表、order by子句以及其他語句部分。

需要注意,表別名只在查詢執行中使用。與列別名不一樣,表別名不返回到客戶端。

自聯結假設要給jim jones同一公司的所有顧客傳送一封信件。

a、子查詢方法:

select cust_id, cust_name, cust_contact

from customers

where cust_name = (select cust_name

from customers

where cust_contact = 'jim jones');

b、自聯結方法:

select c1.cust_id, c1.cust_name, c1.cust_contact

from customers as c1, customers as c2

where c1.cust_name = c2.cust_name

and c2.cust_contact = 'jim jones';

自聯結通常作為外部語句,用來替代從相同表中檢索資料的使用子查詢語句。雖然最終的結果是相同的,但許多dbms處理聯結遠比處理子查詢快得多。

自然聯結

標準的聯結(內聯結)返回所有資料,相同的列甚至多次出現。自然聯結排除多次出現,使每一列只返回一次。

自然聯結要求你只能選擇那些唯一的列,一般通過對乙個表使用萬用字元(select *),而對其他表的列使用明確的子集來完成。

select c.*, o.order_num, o.order_date, oi.prod_id, oi.quantity, oi.item_price

from customers as c, orders as o, orderitems as oi

where c.cust_id = o.cust_id

and oi.order_num = o.order_num

and prod_id = 'rgan01';

外聯結
select customers.cust_id, orders.order_num

from customers inner join orders

on customers.cust_id = orders.cust_id;

外聯結例項:(要檢索包括沒有訂單顧客在內的所有顧客)

select customers.cust_id, orders.order_num

from customers left outer join orders

on customers.cust_id = orders.cust_id;

外聯結包含沒有關聯行的行。在使用outer join語法時,必須使用right或left關鍵字指定包括其所有行的表(right指出的是outer join右邊的表,而left指出的是outer join左邊的表)。

左外聯結與右外聯結之間的唯一差別是所關聯的表的順序。即,調整from或where子句中表的順序,左外聯結可以轉換為右外聯結。

使用帶聚集函式的聯結

select customers.cust_id, 

count(orders.order_num) as num_ord

from customers inner join orders

on customers.cust_id = orders.cust_id

group by customers.cust_id;

這條select語句使用inner join將customers和orders表互相關聯。group by子句按顧客分組資料,因此,函式呼叫count(orders.order_num)對每個顧客的訂單計數,將它作為num_ord返回。

聚集函式也可以方便地與其他聯結一起使用。

select customers.cust_id, 

count(orders.order_num) as num_ord

from customers left outer join orders

on customers.cust_id = orders.cust_id

group by customers.cust_id;

這個例子使用左外聯結來包含所有顧客,甚至包含那些沒有任何訂單的顧客。

使用聯結和聯結條件

a、注意所使用的聯結型別。一般我們使用內聯結,但使用外聯結也有效。

b、保證使用正確的鏈結條件,否則會返回不正確的資料。

c、應該總是提供聯結條件,否則會得出笛卡兒積。

d、在乙個聯結中可以包含多個表,甚至可以對每個聯結採用不同的聯結型別。但應該在一起測試它們之前分別測試每個聯結。

子查詢 聯結表 建立高階聯結 組合查詢

巢狀在其他查詢中的查詢 利用子查詢過濾select cust id from orders where order num in select order num from orderitems where prod id tnt2 作為計算字段使用子查詢select cust name,cust ...

MySQL子查詢,聯結表

子查詢 select cust id from orders where order num in select order num from orderitems where prod id tnt2 對每個客戶執行count 計算,應該將count 作為乙個子查詢 select cust nam...

SQL學習筆記之建立高階聯結

列別名使用方法 列的表示式 對列聚集函式 對列使用其他函式 as 列別名 表別名使用方法 在from子句中,表名 as 表別名 給表起別名有兩個優點 縮短sql語句,使得語法更簡潔 可以在select語句中多次使用相同的表 select cust name,cust contact from cus...