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

2021-09-27 08:47:17 字數 4060 閱讀 7453

巢狀在其他查詢中的查詢

利用子查詢過濾

select cust_id 

from orders

where order_num in (select order_num

from orderitems

where prod_id ='tnt2');

作為計算字段使用子查詢
select cust_name,

cust_state.

(select count(*)

from orders

where order.cust_id =costomers.cust_id) as oreders

from customers

oeder by cust_name;

上述舉例採用了相關子查詢— 涉及外部查詢的子查詢

使用了完全限定列名(表名和列名由乙個句點分隔)

理解關係表的設計就是要保證把資訊分解成多個表,一類資料乙個表。各個表通過某些常用的值互相關聯

外來鍵為某個表中的一列,它包含另乙個表的主鍵值,定義了兩個表之間的關係

優點:

資訊不重複,節省時間和空間

變動單錶單個資訊,相關表資料不用改動 資料是高度一致的,處理資料更簡單

可伸縮性(scale)能適應不斷增加的工作量而不失敗。涉及良好的資料庫或應用程式稱之為可伸縮性好

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

注意:聯結不是物理實體,它在實際的資料庫中不存在,而是存在於查詢執行當中

建立聯結

使用了完全限定名

指定的列出現在不同的表中

where子句重要性→ where子句作為過濾條件,它只包含哪些匹配給定條件(這裡是聯結條件)的行。沒有where子句則第乙個表的每一行和第二個表每一行配對,而不管邏輯上是否可以匹配

笛卡爾積由沒有聯結條件的表關係返回的結果為笛卡爾積。檢索出的行的資料未第乙個表的行數與第二個錶行數的乘積

select vend_name,prod_name,prod_price

from vendors,products

where vendors.vend=products.vend

order by vend_name,prod_name

內部聯結

基於兩個表之間的相等測試,這種聯結叫做等值聯結,又叫內部聯結

語法:inner join on

select vend_name,prod_name,prod_price

from vendors inner join products

on vendors.vend_id=products.vend_id;

sql對一條select語句中可以聯結的表的數目沒有限制

select prod_name,vend_name,prod_price,quantity

from orderitems,products,vendors

where products.vend_id=vendors.vend_id

and orderitems.prod_id=products.proid_id

and order_num=2005;

效能考慮mysql在執行時關聯指定的每個表以處理聯結。這種處理可能是非常耗費資源的。因此不要聯結不必要的表,聯結的越多效能下降越厲害

在聯結表時使用表別名、聚集函式以及不同型別的聯結

使用表別名

優勢:

縮短sql語句;

允許在單條select語句中多次使用相同的表;

select cust_name ,cust_concat

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 = 'tnt2';

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

不同型別的聯結

自聯結、自然聯結 和 外部聯結

自聯結在單條select 語句中不知一次引用相同的表

select p1.prod_id,p1.prod_name

from products as p1,products as p2 #product表出現兩次

where p1.vend_id = p2.vend_id #聯結兩個表

and p2.prod_id = 'dintr' ; #過濾資料

解析:product表在from子句**現2次雖然是合法的,但對此表的引用具有第二性,因為mysql不知道你引用的是表中哪個例項。

因此為了解決次問題,使用了表別名

注意: 自聯結通常作為外部語句用來替代從相同表中檢索資料時使用的字查詢語句。結果相同,但有時候處理聯結比處理子查詢更快

外部聯結

聯結包含了那些在相關表中沒有關聯的行,這種型別的聯結稱為外部聯結

語法:outer join on

關鍵字:left/right

在使用outer join 語法時,必須使用left/right關鍵字指定包括其所有行的表(right指出的是outer join右邊的表),下面的例子選中的是左邊customers表

select customers.cust_id ,orders.order_num

from customers left outer join orders

on customers.cust_id=orders.cust_id;

使用聚集函式聯結

聚集函式用來彙總資料,不近可以從單個表彙總資料,也可以與聯結一起使用(內外聯結都可以)

select customers.cust_name,

customers.cust_id,

count(order.order_num) as num_ord

from customers left outer join orders

group by customers.cust_id;

聯結總結利用union操作符將多條select預計組合成乙個結果集

mysql允許執行多個查詢(多條select語句),並將結果作為單個查詢結果集返回。這些組合查詢通常稱為並(union)或復合查詢

使用組合查詢情況:

#組合查詢和多個where條件

組合查詢

select vend_id,prod_id,proid_price

from products

where prod_price<=5

union

select vend_id,prod_id,proid_price

from products

where vend_id in (1001,1002)

多個where條件

select vend_id,prod_id,proid_price

from products

where prod_price<=5

or vend_id in(1001,1002)

union規則

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

利用子查詢進行過濾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 ...

MySQL子查詢,聯結表

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

資料庫9 聯結表 高階聯結 組合查詢 全文本搜尋

sql最強大的功能之一就是能在資料檢索查詢的執行中聯結 join 表。聯結是利用sql的select能執行的最重要的操作,能很好的理解聯結及其語法是學習sql的乙個極為重要的組成部分。1 注意所使用的聯結型別,一般我們使用內部聯結,但使用外部聯結也是有效的。2 保證使用正確的聯結條件,否則將返回不正...