在mysql中實現先分組后排序的方法
在mysql中實現按實際大小進行排序的方法
1.首先,在命令行中啟動MySQL服務;
service mysql start
2.MySQL服務啟動后,在命令行中輸入mysql的用戶名和密碼登錄到MySQL;
mysql -u root -p
3.登錄到MySQL后,選擇使用一個數據庫;
use mysql;
4.最后,進入到數據庫后,對數據表中的數據進行先分組后排序;
如何表結構如下:
+----+------+--------+--------------+
| id | Name | Salary | Departmentld |
+----+------+--------+--------------+
| 1 | Joe | 70000 | 1 |
| 2 | Henry| 80000 | 2 |
| 3 | Sam | 60000 | 2 |
| 4 | Mark | 90000 | 1 |
+----+------+--------+--------------+
執行以下命令即可實現先分組后排序
SELECT
a.max AS salary,
b.`name` AS employee,
c.depName AS departId
FROM
(SELECT MAX(salary) max FROM cs_employee t WHERE t.departId is NOT NULL GROUP BY t.departId ) a
LEFT JOIN cs_employee b ON a.max = b.salary
LEFT JOIN cs_depart c ON b.departId = c.id
ORDER BY b.departId