在mysql中實(shí)現(xiàn)先分組后排序的方法
在mysql中實(shí)現(xiàn)按實(shí)際大小進(jìn)行排序的方法
1.首先,在命令行中啟動(dòng)MySQL服務(wù);
service mysql start
2.MySQL服務(wù)啟動(dòng)后,在命令行中輸入mysql的用戶名和密碼登錄到MySQL;
mysql -u root -p
3.登錄到MySQL后,選擇使用一個(gè)數(shù)據(jù)庫(kù);
use mysql;
4.最后,進(jìn)入到數(shù)據(jù)庫(kù)后,對(duì)數(shù)據(jù)表中的數(shù)據(jù)進(jìn)行先分組后排序;
如何表結(jié)構(gòu)如下:
+----+------+--------+--------------+
| id | Name | Salary | Departmentld |
+----+------+--------+--------------+
| 1 | Joe | 70000 | 1 |
| 2 | Henry| 80000 | 2 |
| 3 | Sam | 60000 | 2 |
| 4 | Mark | 90000 | 1 |
+----+------+--------+--------------+
執(zhí)行以下命令即可實(shí)現(xiàn)先分組后排序
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