03、MySQL連線查詢之內連線
阿新 • • 發佈:2020-07-23
在連線查詢之前,同樣匯入一份sql檔案。
連線查詢
笛卡爾乘積:表一有m行,表二有n行,結果 = m*n 行。
內連線
#進階七、連線查詢、多表查詢 /* 當我們要查詢的欄位涉及多張表時,這時我們就會用到連線查詢。 */ #笛卡爾乘積 SELECT NAME, boyname FROM beauty, boys; #正確的寫法(等值連線) SELECT NAME, boyname FROM beauty, boys WHERE beauty.`boyfriend_id` = boys.`id`; /* 連線查詢的分類: 1、按年代分 SQL92標準:在MySQL中僅僅支援內連線 SQL99標準(推薦):支援所有內連線、外連線(左外連線、右外連線)、交叉連線 2、按功能分 內連線 等值連線 非等值連線 自連線 外連線 左外連線 右外連線 全外連線 交叉連線 */ #一、---------------------------------SQL92語法---------------------------------- #內連線 -- 1、等值連線 /* 語法: select 查詢列表 from 表名1, 表名2, ... where 等值連線條件; 1、因為會涉及多張表,容易造成欄位名重複,為了解決這個問題,往往為表起別名; 2、from子句表的順序無要求; */ #查詢員工名和部門名 SELECT last_name, department_name FROM employees e, departments d WHERE e.`department_id` = d.`department_id`; #查詢部門編號>100的部門名和城市名 SELECT department_id, city FROM departments d, locations l WHERE d.`department_id` > 100 AND d.`location_id` = l.`location_id`; #查詢有獎金的員工名和部門名 SELECT last_name, department_name FROM employees e, departments d WHERE e.`commission_pct` IS NOT NULL AND e.`department_id` = d.`department_id`; #查詢城市名第二個字元為o的部門名和城市名 SELECT department_name, city FROM departments d, locations l WHERE l.`city` LIKE '_o%' AND d.`location_id` = l.`location_id`; #查詢每個城市的部門個數 SELECT COUNT(*) 部門個數, d.`location_id`, l.`city` FROM departments d, locations l WHERE d.`location_id` = l.`location_id` GROUP BY d.`location_id`; #查詢有獎金的每個部門的部門名和部門的領導編號和該部門的最低工資 SELECT d.department_name, d.manager_id, MIN(salary) FROM employees e, departments d WHERE e.`commission_pct` IS NOT NULL AND e.`department_id` = d.`department_id` GROUP BY e.`department_id`; #查詢每個工種的工種名和員工的個數,並且按員工個數降序 SELECT job_title, COUNT(*) 員工個數 FROM employees e, jobs j WHERE e.`job_id` = j.`job_id` GROUP BY e.`job_id` ORDER BY 員工個數 DESC; #三表連線:查詢員工名、部門名和所在的城市 SELECT last_name, department_name, city FROM employees e, departments d, locations l WHERE e.`department_id` = d.`department_id` AND d.`location_id` = l.`location_id`; -- 2、非等值連線 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); #查詢出員工的工資和工資級別 SELECT salary, grade_level FROM employees e, job_grades j WHERE (e.`salary` BETWEEN j.`lowest_sal` AND j.`highest_sal`) AND grade_level IN ('A', 'B', 'C') ORDER BY salary DESC; -- 3、自連線 #相當於等值連線,只不過涉及到的表只有自己一張 #查詢員工名以及他的領導的名稱 SELECT e1.`last_name`, e1.`manager_id`, e2.`last_name`, e2.`employee_id` FROM employees e1, employees e2 WHERE e1.`manager_id` = e2.`employee_id` ORDER BY e1.`last_name`; #二、---------------------------------SQL99語法---------------------------------- #內連線 /* 語法: select 查詢列表 from 表1 別名1 [連線型別] join 表2 別名2 on 連線條件 where 篩選條件; 內連:[inner] 左外:left [outer] 右外:right [outer] 全外:full [outer] 交叉:cross */ -- 1、等值連線 #查詢員工名、部門名 SELECT last_name, department_name FROM employees e INNER JOIN departments d ON e.`department_id` = d.`department_id`; #查詢名字中包含e的員工名和工種名 SELECT last_name, job_title FROM employees e INNER JOIN jobs j ON e.`job_id` = j.`job_id` WHERE e.`last_name` LIKE '%e%'; #查詢部門個數大於3的部門個數和城市名 SELECT COUNT(*) 部門個數, l.`city` FROM departments d INNER JOIN locations l ON d.`location_id` = l.`location_id` GROUP BY d.`location_id` HAVING COUNT(*) > 3; #查詢哪個部門的員工個數>3的部門名和員工個數,並按個數降序 SELECT department_name 部門名, COUNT(*) 員工個數 FROM employees e INNER JOIN departments d ON e.`department_id` = d.`department_id` GROUP BY e.`department_id` HAVING COUNT(*) > 3 ORDER BY 員工個數 DESC; #查詢員工名、部門名、工種名,並按部門名降序 SELECT last_name, department_name, job_title FROM employees e INNER JOIN departments d ON e.`department_id` = d.`department_id` INNER JOIN jobs j ON e.`job_id` = j.`job_id` ORDER BY department_name DESC; -- 2、非等值連線 #查詢員工工資和工資級別 SELECT last_name, salary, grade_level FROM employees e JOIN job_grades j ON e.`salary` BETWEEN j.`lowest_sal` AND j.`highest_sal`; #查詢每個工資級別的個數,並按照級別降序 SELECT last_name, salary, grade_level, COUNT(*) 個數 FROM employees e JOIN job_grades j ON e.`salary` BETWEEN j.`lowest_sal` AND j.`highest_sal` GROUP BY grade_level ORDER BY grade_level DESC; -- 3、自連線 #查詢員工的名字和領導的名字 SELECT e1.last_name, e1.`manager_id`, e2.`last_name`, e2.`employee_id` FROM employees e1 JOIN employees e2 ON e1.`manager_id` = e2.`employee_id`;