大家好,這裡是快忘光SQL的小編過路君子
看了看紀錄,上次寫這麼複雜的SQL已經是在一年之前
SQL 小編說常用也常用,說不常用也不常用,為什麼這麼說呢?
因為平常使用的時候就只會用基本的 SELECT 功能,什麼 sub select 根本就不會用到。
今天回來複習一下 MySQL 的 SQL 語法
那在開始之前,先假設我們有以下的資料表:
Table: Employee +--------------+---------+ | Column Name | Type | +--------------+---------+ | id | int | | name | varchar | | salary | int | | departmentId | int | +--------------+---------+ Table: Department +-------------+---------+ | Column Name | Type | +-------------+---------+ | id | int | | name | varchar | +-------------+---------+
並且具有以下的內容:
Employee table: +----+-------+--------+--------------+ | id | name | salary | departmentId | +----+-------+--------+--------------+ | 1 | Joe | 85000 | 1 | | 2 | Henry | 80000 | 2 | | 3 | Sam | 60000 | 2 | | 4 | Max | 90000 | 1 | | 5 | Janet | 69000 | 1 | | 6 | Randy | 85000 | 1 | | 7 | Will | 70000 | 1 | +----+-------+--------+--------------+ Department table: +----+-------+ | id | name | +----+-------+ | 1 | IT | | 2 | Sales | +----+-------+
不考慮資料重複或並列
我們則需要輸出以下的資料:
+------------+----------+--------+ | Department | Employee | Salary | +------------+----------+--------+ | IT | Max | 90000 | | IT | Joe | 85000 | | IT | Randy | 85000 | | Sales | Henry | 80000 | | Sales | Sam | 60000 | +------------+----------+--------+
簡單來說就是將 departmentId 轉換成對應的部門名稱,然後要將每個前三高薪水(Salary)的人列出,最後在對資料做排序。
直接上 SQL 語法:
SELECT c.name AS Department, a.name AS Employee, a.salary AS Salary FROM Employee AS a LEFT JOIN Department AS c ON c.id = a.departmentId WHERE ( SELECT COUNT(b.salary) FROM Employee AS b WHERE b.departmentId = a.departmentId AND a.salary < b.salary ) < 3 ORDER BY c.id, a.salary;
輸出結果就會如上方所示。
其中的 LEFT JOIN 作用就是為了要讓 Department 表裡的資料直接接在 Employee 表的後面,只有當 Department 表內的 id 和 Employee 表內的 departmentId 相同才需要做串接。
而比較重要的就是 WHERE 內的 sub select 了,此 sub select 會試著去統計在 Employee 表內目前有幾個人的薪水大於目前所執行到人的薪水。
而最後的小於三則是取前三高,如果要取前五高就改成 5 即可。
考慮資料重複或並列
若需考慮資料重複,那我們要輸出的資料變成如下:
+------------+----------+--------+ | Department | Employee | Salary | +------------+----------+--------+ | IT | Max | 90000 | | IT | Joe | 85000 | | IT | Randy | 85000 | | IT | Will | 70000 | | Sales | Henry | 80000 | | Sales | Sam | 60000 | +------------+----------+--------+
這應該才算是比較常用到的手段,總之寫法如下:
SELECT c.name AS Department, a.name AS Employee, a.salary AS Salary FROM Employee AS a LEFT JOIN Department AS c ON c.id = a.departmentId WHERE ( SELECT COUNT(b.salary) FROM (SELECT DISTINCT d.departmentId, d.salary FROM Employee AS d) AS b WHERE b.departmentId = a.departmentId AND a.salary < b.salary ) < 3; ORDER BY c.id, a.salary;
只需要改一行就可以了。
簡單來說,在開始找有幾個人薪水大於目前的人之前,先把要比較的表去除重複的資料,這樣統計的時候也不會重複統計啦。
後記
總之這次寫完感覺這個 SQL 很有用,所以就在這邊留存一份,省的未來在那邊推敲半天。