Mysql學習 第十三章 高階聯結

2021-10-23 21:57:29 字數 1995 閱讀 5413

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

縮短 sql語句;

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

select cust_name, cust_contact

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

自聯結(self-join)、自然聯結(natural join)和外聯結(outer join)。

自聯結

select cust_id, cust_name, cust_contact

from customers

where cust_name = (select cust_name

from customers

where cust_contact = 'jim jones');

等於以下**

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

#c2.cust_contact = 'jim jones'限制公司名字

#c1.cust_name = c2.cust_name提取c1資料

自然聯結

自然聯結要求你只能選擇那些唯一的列,一般通過對乙個表使用萬用字元( 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 left outer join orders

on customers.cust_id = orders.cust_id;

在使用 outer join 語法時,必須使用 right 或 left 關鍵字指定包括其所有行的表( right 指出的是 outer join 右邊的表,而 left 指出的是 outer join左邊的表)。上面的例子使用 left outer join 從 from 子句左邊的表( customers 表)中選擇所有行。

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;

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

SQL必知必會 筆記 第十三章 建立高階聯結

別名除了用於列名和計算欄位外,sql還允許給表明起別名。這樣做有兩個主要理由 1 縮短sql語句 2 允許在單條select語句中多次使用相同的表。select cust name,cust contact from customers as c,orders as o,orderitems as ...

第十三章 學習 Shell Scripts

什麼是 shell scripts shell script 程式化指令碼 shell script 是針對 shell 所寫的 指令碼!shell script 是利用 shell 的功能所寫的乙個 程式 program 這個程式是使用純文字檔,將一些 shell 的語法與命令 含外部命令 寫在裡...

第十三章 併發

13.1 動機 13.2 基本執行緒 如果必須要控制現成的執行順序,最好是根本不用執行緒,而是自己編寫特定順序彼此控制的協作子程式。繼承thread類或者實現runnable介面。內部類實現。13.3 共享受限資源 1 如果要對類中的某個方法進行同步控制,最好同步所有方法。如果忽略了其中乙個,通常很...