時光在不經意間,總是過得出奇的快。小暑已過,進入中暑,太陽更加熱烈的綻放著ta的光芒,...在外面被太陽照顧的人們啊,你們都是勤勞與可愛的人啊。在房子里已各種姿勢看我這篇這章的你,既然點了進來,那就由我繼續帶你回顧MySql的知識吧! 回顧練習資料girls庫以及兩張表的腳本: https://pa ...
時光在不經意間,總是過得出奇的快。小暑已過,進入中暑,太陽更加熱烈的綻放著ta的光芒,...在外面被太陽照顧的人們啊,你們都是勤勞與可愛的人啊。在房子里已各種姿勢看我這篇這章的你,既然點了進來,那就由我繼續帶你回顧MySql的知識吧!
回顧練習資料girls庫以及兩張表的腳本: https://pan.baidu.com/s/1NAvIOIrIPB8ZVfRblU2ppg 提取碼: rrwa
引題:笛卡爾現象,先來觀看一下兩張表。
SELECT * FROM boys;
SELECT * FROM beauty;
SELECT NAME,boyname FROM boys,beauty;
最終結果:12*4=48行
#進階6:連接查詢
含義:又稱多表查詢,當查詢的欄位來自於多個表時,就會用到連接查詢
笛卡兒積現象: 表1 有m行,表2有n行,結果=m*n行
產生原因:沒有有效的連接條件
解決方法:添加有效的連接條件
連接分類:
按年代分類:
SQL1992標準(192標準):僅支持內連接
SQL1999標準(199標準) [推薦]:支持內連接+外聯結(左外與右外)+交叉連接
按功能分類:
內連接:
等值連接
非等值連接
自聯結
外連接:
左外連接
右外連接
全外連接
交叉連接:
左外連接
右外連接
全外連接
交叉連接:
SELECT NAME,boyname FROM boys,beauty
WHERE beauty.boyfriend_id = boys.id;
#一.SQL192標準
#1.等值連接
①多表等值連接的結果為多表的交集部分
②n表連接,至少需要n-1個連接條件
③多表的順序沒有要求
④一般需要為表起別名
⑤可以搭配前面介紹的所有子句使用,比如,排序,分組,篩選。
#多表查詢,先匹配在篩選
#案例1.查詢員工名和對應的部門名。
SELECT first_name AS 名,department_name AS 部門名
FROM employees,departments
WHERE employees.department_id = departments.department_id;
#案例2.查詢員工名,工種號,工種名。對於兩張表共有的欄位需要加表名作限定不然會報錯。
錯誤示例:
SELECT first_name AS 名,employees.job_id AS 工種號,job_title AS 工種名
FROM employees,jobs
WHERE employees.job_id = jobs.job_id;
#2.為表起別名
①提高語句的簡潔度
②區分多個重命名的欄位
註意:如果為表起了別名,則查詢的欄位就不能使用原來的表名去限定
SELECT first_name AS 名,e.job_id AS 工種號,job_title AS 工種名
FROM employees AS e,jobs AS j
WHERE e.job_id = j.job_id;
給表起了別名,再用表完整名子做限定會報錯,不允許。根據執行順序走先走FROM,
走完FROM後就用別名的,相當於生成了一個虛擬的視圖,不再認原來的表名。
#3.兩個表名的順序是否可以調換,是可以調換的。
SELECT first_name AS 名,e.job_id AS 工種號,job_title AS 工種名
FROM jobs AS j,employees AS e
WHERE e.job_id = j.job_id;
#4.可以加篩選
#案例3.查詢有獎金的員工名,部門名。
SELECT first_name AS 名,department_name AS 部門名,commission_pct AS 獎金
FROM employees AS e,departments AS d
WHERE e.department_id=d.department_id
AND commission_pct IS NOT NULL;
#AND e.commission_pct IS NOT NULL;
#案例4.查詢城市名中第二個字元為o對應的城市名與部門名。
SELECT city AS 城市,department_name AS 部門名
FROM locations AS l,departments AS d
WHERE l.location_id = d.location_id
AND city LIKE '_o%';
#5.可以加分組
#案例1.查詢每個城市的部門個數。
SELECT city AS 城市,COUNT(department_id) AS 個數
FROM locations AS l,departments AS d
WHERE l.location_id = d.location_id
GROUP BY l.city;
#案例2.查詢有獎金的每個部門的部門名和部門的領導編號和該部門的最低工資。
#查詢的時候不確定把兩個列都加上。
SELECT commission_pct AS 獎金,department_name AS 部門名,
d.manager_id AS 領導編號,MIN(salary) AS 最低工資
FROM employees AS e,departments AS d
WHERE e.department_id = d.department_id
AND commission_pct IS NOT NULL
GROUP BY department_name,d.manager_id;
#6.可以加排序
#案例1:查詢每個工種的工種名和員工的個數,並且按員工個數降序。
SELECT j.job_title AS 工種名,COUNT(employee_id) AS 個數
FROM employees AS e,jobs AS j
WHERE e.job_id = j.job_id
GROUP BY job_title
ORDER BY 個數 DESC;
#7.三表連接
#案例1.查詢員工名,部門名與所在的城市
SELECT first_name AS 名,d.manager_id AS 部門名,city AS 城市
FROM employees AS e,departments AS d,locations AS l
WHERE e.department_id = d.department_id
AND d.location_id = l.location_id;
#案例2.查詢員工名,部門名與所在的城市,城市以s開頭。
SELECT first_name AS 名,d.manager_id AS 部門名,city AS 城市
FROM employees AS e,departments AS d,locations AS l
WHERE e.department_id = d.department_id
AND d.location_id = l.location_id
AND city LIKE 's%';
#案例3.查詢員工名,部門名與所在的城市,城市以s開頭,按姓名降序排列。
SELECT first_name AS 名,d.manager_id AS 部門名,city AS 城市
FROM employees AS e,departments AS d,locations AS l
WHERE e.department_id = d.department_id
AND d.location_id = l.location_id
AND city LIKE 's%'
ORDER BY department_name DESC;
新增一張工資等級表。 CREATE TABLE job_grades (grade_level VARCHAR(3), lowest_sal int, highest_sal int); INSERT INTO job_grades VALUES ('A', 1000, 2999); INSERT INTO job_grades VALUES ('B', 3000, 5999); INSERT INTO job_grades VALUES('C', 6000, 9999); INSERT INTO job_grades VALUES('D', 10000, 14999); INSERT INTO job_grades VALUES('E', 15000, 24999); INSERT INTO job_grades VALUES('F', 25000, 40000);
#2.非等值連接,(範圍判斷)
#案例1.查詢員工的工資和工資級別。
SELECT salary AS 工資,grade_level AS 等級
FROM employees AS e,job_grades AS g
WHERE salary BETWEEN g.lowest_sal AND g.highest_sal;
#案例2.查詢員工的工資和工資級別,展示出A級別的員工。
SELECT salary AS 工資,grade_level AS 等級
FROM employees AS e,job_grades AS g
WHERE salary BETWEEN g.lowest_sal AND g.highest_sal
AND g.grade_level = 'A';
#3.自聯接[自己連接自己]
#案例1.查詢員工名和上級的名稱.
SELECT e.employee_id,e.last_name AS 員工,
m.employee_id,m.last_name AS 領導
FROM employees e,employees m
WHERE e.manager_id = m.employee_id;
經過了以上的示例,相信你對多表查詢已經有了一個瞭解,趕快動動你的小手手來練習一下吧!o(^▽^)o