【发布时间】:2018-08-28 06:09:52
【问题描述】:
我的表教练如下:
+-------+------------+------------+----------+
| ID | name | dept_name | salary |
+-------+------------+------------+----------+
| 10101 | Srinivasan | Comp. Sci. | 65000.00 |
| 12121 | Wu | Finance | 90000.00 |
| 15151 | Mozart | Music | 40000.00 |
| 22222 | Einstein | Physics | 95000.00 |
| 32343 | El Said | History | 60000.00 |
| 33456 | Gold | Physics | 87000.00 |
| 45565 | Katz | Comp. Sci. | 75000.00 |
| 58583 | Califieri | History | 62000.00 |
| 76543 | Singh | Finance | 80000.00 |
| 76766 | Crick | Biology | 72000.00 |
| 83821 | Brandt | Comp. Sci. | 92000.00 |
| 98345 | Kim | Elec. Eng. | 80000.00 |
+-------+------------+------------+----------+
查询
select dept_name, max(salary) from instructor group by dept_name;
会给我导师每个部门的最高薪水。
但是,我想获取结果中的所有列,即每个部门中薪水最高的教师的 ID 和 name。但我不知道该怎么做。
【问题讨论】:
-
此类问题已在 SO 中多次回答。如果你搜索 greatest per group mysql 你会得到很多例子。
-
@GiorgosBetsos This is the usual duplicate 人们正在标记。但是,接受的答案是糟糕的 IMO,你真的必须向下滚动一段时间才能找到看起来不错的东西。