武大 50 名學生將衛星送上天:用了老師 800 萬元科研經費,搭長征八號“順風車”升空
阿新 • • 發佈:2022-02-28
多表查詢
案列說明
笛卡爾積的理解
select id,department_name
from employees,departments;#錯的
select id,department_id,department_name
from employees CROSS JOIN departments;#錯的
每個員工和每個部門匹配了一遍(查出的條目數=id數*department數)
錯誤原因:缺少連線條件
笛卡爾積的解決
編寫連線條件: 表1.列 = 表二.列(若多個表連線,至少要用n-1個連線條件)
select id,employees.name,department_name from employees,departments WHERE employees.name = departments.name;
注:如果要顯示的列在要查詢的表中名字一樣,則要表明,是出自哪個表, eg: employees.name
建議在多表查詢時,標明顯示的是哪個表的資訊 (優化)
優化:可以在FROM後使用表的別名,但是一旦使用別名,後續就一定要都用別名
多表查詢的分類
等值連線和非等值連線
-
等值連線:上述的帶有=的
-
非等值連線:沒有=的
select t1.id,t1.name,t2.grade from employees t1,departments t2 WHERE ti.salary BETWEEN t2.lowest_salary AND t2.highest_salary ;#非等值
自連線和非自連線
-
非自連結:表1和表2連線
-
自連結:表1和自己連線
#顯示員工(t1)和其管理者(t2)的基本資訊
select t1.id,t1.name,t2.id,t2.name
from employees t1,employees t2#一個表看作兩個表
WHERE t1.manage_id = t2.id ;#自連線
內連線和外連線
-
內連線:合併含有同一列的表,結果不包括一個表與另一個表不匹配打的行
-
外連線:合併含有同一列的表,結果除了內連線的結果還查詢不匹配的行
外連線的分類:左外連線(左表多,不右邊),右外連線(右表多補左邊),滿外連線
SQL92:使用(+)建立連線
內連線:見上
外連線:左表有資料不匹配,在右表加(+);反之,在左表加(+),但是MySQL不支援
WHERE t1.department_id = t2.department_id(+)#左連線
SQL99:使用JOIN...ON的方式
內連線
select t1.id,t1.name,t2.department_name,t3.environment
from employees t1 JOIN departments t2
ON t1.department_id = t2.department_id
JOIN locations t3#加入第二個人表
ON t2.department_location = t3.department_location;
外連線
使用OUTER JOIN...ON...
- 左外連線:LEFT OUTER JOIN
- 右外連線:RIGHT OUTER JOIN
- 滿外連線:FULL OUTER JOIN
select t1.name,t2.department_name#左外連線
from employees t1 LEFT OUTER(可省略) JOIN departments t2
ON t1.department_id = t2.department_id;
UMION的使用
合併查詢結果
SELECT colum... FROM table1
UNION (ALL)
SELECT colum... FROM table2
- UNION操作符
兩個查詢結果的並集,去重(效率低)
- UNION ALL操作符(推薦)
兩個查詢結果的並集,不去重(效率高)
7種SQL JOINS的實現
中圖(內連線):
select t1.name,t2.department_name
from employees t1 JOIN departments t2
ON t1.department_id = t2.department_id;
左上圖(左外連線):
select t1.name,t2.department_name
from employees t1 LEFT JOIN departments t2
ON t1.department_id = t2.department_id;
右上圖(右外連線):
select t1.name,t2.department_name
from employees t1 RIGHT JOIN departments t2
ON t1.department_id = t2.department_id;
左中圖:
select t1.name,t2.department_name
from employees t1 LEFT JOIN departments t2
ON t1.department_id = t2.department_id
WHERE t2.department_id IS NULL;
右中圖:
select t1.name,t2.department_name
from employees t1 RIGHT JOIN departments t2
ON t1.department_id = t2.department_id
WHERE t1.department_id IS NULL;
左下圖(滿外連線):
#方式一:左上圖 UNION ALL 右中圖
select t1.name,t2.department_name
from employees t1 LEFT JOIN departments t2
ON t1.department_id = t2.department_id
UNION ALL
select t1.name,t2.department_name
from employees t1 RIGHT JOIN departments t2
ON t1.department_id = t2.department_id
WHERE t1.department_id IS NULL;
#方式二:左中圖 UNION ALL 右上圖
select t1.name,t2.department_name
from employees t1 LEFT JOIN departments t2
ON t1.department_id = t2.department_id
WHERE t2.department_id IS NULL
UNION ALL
select t1.name,t2.department_name
from employees t1 RIGHT JOIN departments t2
ON t1.department_id = t2.department_id;
右下圖:
#左中圖 UNION ALL 右中圖
select t1.name,t2.department_name
from employees t1 LEFT JOIN departments t2
ON t1.department_id = t2.department_id
WHERE t2.department_id IS NULL
UNION ALL
select t1.name,t2.department_name
from employees t1 RIGHT JOIN departments t2
ON t1.department_id = t2.department_id
WHERE t1.department_id IS NULL;
SQL語法新特性
自然連線
使用關鍵字:NATURAL JOIN(不靈活),自動查詢表中所有相同欄位,然後進行等值連線
USING連線(不適用於自連線)
使用關鍵字:USING(同名欄位),將表中相同名字的欄位自動等值連線
select t1.name,t2.department_name
from employees t1 JOIN departments t2
ON t1.department_id = t2.department_id;
等價於
select t1.name,t2.department_name
from employees t1 JOIN departments t2
USING(department_id);