考虑这个问题源自 LeetCode 上的一道题:184. Department Highest Salary
大概意思就是用下面两张表(Employee、Department)合成最后的一张表,得到以 DepartmentId 分组的 Salary 值最大的记录:
Employee
1 | +----+-------+--------+--------------+ |
Department
1 | +----+----------+ |
最终得到:
1 | +------------+----------+--------+ |
思路其实很清晰:
- 「找最大记录」Employee 表中找到以 DepartmentId 分组的 Salary 值最大的记录
- 「连接」Employee 表与 Department 表连接
或
- 「连接」 Employee 表与 Department 表连接
- 「找最大记录」连接后的表中找到以 DepartmentId 分组的 Salary 值最大的记录
那么最大的问题就停留在:如何找到按字段 1 分组的字段 2 最大的记录?
先后尝试了以下几种方法:
方法一(不可行):1
select * from (select * from table2 order by age desc) as a group by a.table1_id
方法二:1
select a.* from table2 as a where age = (select max(age) from table2 where a.table1_id=table1_id)
方法三:1
select a.* from table2 as a where not exists (select * from table2 where table1_id=a.table1_id and age>a.age)
方法四:1
select a.* from table2 as a where exists (select count(*) from table2 where table1_id=a.table1_id and age>a.age having count(*)=0)
第一种方法不可行的原因是由于违反了 ONLY_FULL_GROUP_BY 模式要求,具体原因参见 SQL_MODE = ONLY_FULL_GROUP_BY。方法二三四亲测都是可行的,那么使用思路一,先找到 Employee 表中以 DepartmentId 分组的 Salary 值最大的记录,再连接的写法如下:
1 | # 方法一 |
1 | # 方法二 |
1 | # 方法三 |