MySQL 多表查詢 內連線 和 外連線
阿新 • • 發佈:2021-09-01
連線查詢(多表查詢)
查詢的欄位來自於多個表中,這個時候需要連線多個表進行查詢。稱為連線查詢
連線查詢的分類
1. 內連線:查詢兩個表的交集
① 等值內連線
③ 自連線
2. 外連線
① 左外連線
② 右外連線
注意
1. 連線查詢 需要建立連線條件
2. 如果沒有連線條件 引發笛卡爾乘積現象
1.隱式內連線
語法: select 查詢列表 from 表1 別名1,表2 別名,... where 連線條件 and 分組前篩選 group by 分組欄位 having 分組後篩選 order by 排序欄位 asc|desc limit 偏移量,個數 等值連線 案例1:查詢員工的名字和其對應部門的名字 select e.*, d.department_name from employees e,departments d where e.department_id = d.department_id; 案例2:查詢有獎金的員工的工種名、名字、郵箱、年薪 select e.last_name 員工名,e.email 郵箱,(salary+commission_pct*salary)*12 年薪, j.job_title 工種名 from employees e,jobs j where e.commission_pct is not null and e.job_id = j.job_id; 案例3: 查詢哪個城市的部門個數 > 2 select d.location_id,count(department_id),l.city from departments d,locations l where d.location_id = l.location_id group by d.location_id having count(department_id) > 2; 案例4:查詢每個工種的工種名 和 平均薪資 並且按照平均薪資 降序 select j.job_title,avg(salary) from employees e,jobs j where e.job_id = j.job_id group by j.job_title order by avg(salary) desc; 案例5: 查詢員工的名字和其對應的部門名、工種名 select e.last_name,d.department_name,j.job_title from employees e, departments d, jobs j where e.department_id = d.department_id and e.job_id = j.job_id;
自連線
案例6:查詢每個員工名字和對應的領導的名字
select e1.last_name as 下屬名字,e2.last_name as 上司名字 from employees e1,employees e2
where e1.manager_id = e2.employee_id;
2.顯式內連線
select 查詢列表 from 表1 別名1 【inner】 join 表2 別名2 on 連線條件 等值內連線 #案例1:查詢員工的名字和其對應部門的名字 select e.last_name,d.department_name from employees e inner join departments d on e.department_id = d.department_id; #案例2:查詢有獎金的員工的名字和對應部門的名字 select e.last_name,d.department_name from employees e inner join departments d on e.department_id = d.department_id where commission_pct is not null; #案例3:查詢每個部門的部門名 和員工的最低薪資,只顯示員工最低薪資都大於5000 的部門 select department_name,min(salary) from employees e inner join departments d on e.department_id = d.department_id group by d.department_name having min(salary) > 5000; #案例4:查詢每個部門的部門名 和員工的最低薪資,只顯示員工最低薪資都大於5000 的部門, #並且按照最低薪資降序 select department_name,min(salary) from employees e inner join departments d on e.department_id = d.department_id group by d.department_name having min(salary) > 5000 order by min(salary) desc; #案例5:查詢員工的名字和其對應領導的名字和薪資 select e1.last_name as 下屬名字,e1.salary 下屬薪資, e2.last_name as 上司名字,e2.salary as 上司薪資 from employees e1 inner join employees e2 on e1.manager_id = e2.employee_id; #案例6: 查詢員工的名字和其對應部門的名字 以及對應工種名字 select e.last_name,d.department_name,j.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;
3.外連線
語法: select 查詢列表 from 表1 別名1 left|right 【outer】 join 表2 別名2 on 連線條件 where 分組前篩選 group by 分組欄位 having 分組後篩選 order by 排序欄位 asc|desc limit 偏移量,個數 特點: 1. 將主表的所有資料都會查詢出來,對應表有對應資料 查詢出來,沒有對應資料 顯示null 2. 主表指 left 左表的表為主表 right右邊的表為主表 3. 查詢結果 = 內連線的結果 + 主表中有 從表中沒有的結果 #案例1:查詢員工的名字和對應部門名字 (使用左外連線查詢 員工表為主表) #外連線 select * from employees e LEFT JOIN departments d on e.department_id = d.department_id; select * from employees e right JOIN departments d on e.department_id = d.department_id; #案例2:查詢哪個員工沒有部門 select * from employees where department_id is null; #案例3:查詢哪些部門沒有員工 select * from departments d left join employees e on d.department_id = e.department_id where employee_id is null;
總結
多表查詢總的來分 有兩種形式
1.內連線
內連線可以細分
1-1.隱式內連線
select * from A,B where 連線條件
1-2.顯示內連線
select * from A inner join B on 連線條件
兩種內連線對查詢結果沒有實質區別,只能顯示符合條件的資料
2.外連線
外連線可以細分
2-1.左外連線
select * from A left join B on 連線條件
A表作為主表,B表作為連線表(對應表).
2-2.右外連線
select * from A right join B on 連線條件
A表作為連線表,B表作為主表
主表顯示所有資料,連線表顯示對應資料,無對應資料使用NUll填充.
兩種外連線對查詢結果有實質區別.