1. 程式人生 > 實用技巧 >資料庫必知必會操作手冊

資料庫必知必會操作手冊

  • 建立高階聯結

1.使用表別名:

mysql> SELECT Concat(vend_name, '(', vend_country, ')') AS vend_title FROM Vendors ORDER BY vend_name;
+------------------------+
| vend_title             |
+------------------------+
| ACME(USA)              |
| Anvils R Us(USA)       |
| Furball Inc.(USA)      |
| Jet Set(England)       |
| Jouets Et Ours(France) | | LT Supplies(USA) | +------------------------+ 6 rows in set (0.00 sec)

我們還可以用AS建立別名:

mysql> 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 = 'ANV01';
+-------------+--------------+
| cust_name | cust_contact | +-------------+--------------+ | Coyote Inc. | Y Lee | +-------------+--------------+ 1 row in set (0.00 sec) mysql>

2.自聯結

自聯結就是自己聯結自己的操作。

SELECT不止一次引用表,如下:

mysql> SELECT cust_id,cust_name,cust_contact FROM Customers WHERE cust_name = (SELECT cust_name FROM
Customers WHERE cust_contact = 'Y Lee'); +---------+-------------+--------------+ | cust_id | cust_name | cust_contact | +---------+-------------+--------------+ | 10001 | Coyote Inc. | Y Lee | +---------+-------------+--------------+ 1 row in set (0.00 sec)

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

mysql> 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 = 'TNT1';
Empty set (0.00 sec)

3.外聯結

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

首先看看內聯結語法,它檢索所有顧客及其訂單:

mysql> SELECT Customers.cust_id,Orders.order_num FROM Customers INNER JOIN Orders ON Customers.cust_id=Orders.cust_id;
+---------+-----------+
| cust_id | order_num |
+---------+-----------+
|   10001 |     20005 |
|   10001 |     20009 |
|   10003 |     20006 |
|   10004 |     20007 |
|   10005 |     20008 |
+---------+-----------+
5 rows in set (0.00 sec)

外聯結的話,是這樣的:(檢索出了沒有訂單在內的所有顧客)

mysql> SELECT Customers.cust_id,Orders.order_num FROM Customers LEFT OUTER JOIN Orders ON Customers.cust_id=Orders.cust_id;
+---------+-----------+
| cust_id | order_num |
+---------+-----------+
|   10001 |     20005 |
|   10001 |     20009 |
|   10002 |      NULL |
|   10003 |     20006 |
|   10004 |     20007 |
|   10005 |     20008 |
+---------+-----------+
6 rows in set (0.00 sec)

外聯結通過關鍵字LEFT OUTER JOIN或者RIGHT OUTER JOIN來指定聯結型別。(RIGHT 指出的是 OUTER JOIN 右邊的表,而 LEFT 指出的是 OUTER JOIN 左邊的表)。

上面的例子使用 LEFT OUTER JOIN 從 FROM 子句左邊的表 (Customers 表)中選擇所有行。

下面的例子使用RIGHTOUTER JOIN 從FROM字句右邊的表中選擇所有行。

mysql> SELECT Customers.cust_id,Orders.order_num FROM Customers RIGHT OUTER JOIN Orders ON Customers.cust_id=Orders.cust_id;
+---------+-----------+
| cust_id | order_num |
+---------+-----------+
|   10001 |     20005 |
|   10001 |     20009 |
|   10003 |     20006 |
|   10004 |     20007 |
|   10005 |     20008 |
+---------+-----------+
5 rows in set (0.00 sec)

4.全聯結

它檢索兩個表中的所有行並關聯那些可以關聯的行。與左外聯結或右外聯結包含一個表的不關聯的行不同,全外聯結包含兩個表的不關聯的行。然而MySql並不支援。

mysql> SELECT Customers.cust_id,Orders.order_num FROM Orders FULL OUTER JOIN Customers ON Orders.cust_id=Customers.cust_id;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'OUTER JOIN Customers ON Orders.cust_id=Customers.cust_id' at line 1
mysql>

5.使用帶聚集函式的聯結

這條 SELECT 語句使用 INNER JOIN 將Customers 和Orders 表互相關聯。 GROUP BY 子句按顧客分組資料,因此,函式呼叫 COUNT(Orders.order_num) 對每個顧客的訂單計數,將它作為 num_ord 返回。

mysql> 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;
+---------+---------+
| cust_id | num_ord |
+---------+---------+
|   10001 |       2 |
|   10003 |       1 |
|   10004 |       1 |
|   10005 |       1 |
+---------+---------+
4 rows in set (0.00 sec)

再有,用LEFT OUTER JOIN和COUNT函式,左外部聯結來包含所有顧客,甚至包含那些沒有任何訂單的顧客。

mysql> 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;
+---------+---------+
| cust_id | num_ord |
+---------+---------+
|   10001 |       2 |
|   10002 |       0 |
|   10003 |       1 |
|   10004 |       1 |
|   10005 |       1 |
+---------+---------+
5 rows in set (0.00 sec)

參考:https://blog.csdn.net/weixin_37972723/article/details/79855381