標籤:外串連 class 1.2 inner 類型 篩選 sql基礎 like select
1 SQL991.1 SQL99文法
select 查詢列表from 表1 [連線類型] join 表2 on 串連條件where 篩選條件;
- 連線類型:
- 內串連 :inner
- 外串連:
- 左外串連: left outer
- 右外串連:right outer
- 交叉串連:cross
1.2 SQL99文法的內串連1.2.1 內串連的文法
select 查詢列表from 表1 inner join 表2 on 串連條件[where 篩選條件];
1.2.2 應用
SELECT e.last_name, d.department_nameFROM employees eINNER JOIN departments d ON e.department_id = d.department_id;
SELECT e.last_name, j.job_titleFROM employees eINNER JOIN jobs j ON e.job_id = j.job_idWHERE e.last_name LIKE ‘%e%‘;
SELECT l.city, count(*) `count`FROM departments dINNER JOIN locations l ON d.location_id = l.location_idGROUP BY l.cityHAVING `count` > 3;
- 樣本:查詢那個部門的員工個數>3的部門名和員工個數,並按照個數降序
SELECT d.department_name, count(*) 員工個數FROM employees eINNER JOIN departments d ON e.department_id = d.department_idGROUP BY d.department_nameHAVING 員工個數 > 3;
- 樣本:查詢員工名、部門名、工種名,並按照部門名降序
SELECT e.last_name 員工名, d.department_name 部門名, j.job_title 工種名FROM employees eINNER JOIN departments dINNER JOIN jobs j ON e.department_id = d.department_idAND e.job_id = j.job_idORDER BY 部門名;
MySQL基礎三