一张表中根据一个字段分组,查询分组中另一个字段最大值
表:
id | score | uid |
---|---|---|
1 | 100 | 1 |
2 | 100 | 1 |
3 | 90 | 1 |
4 | 80 | 2 |
5 | 100 | 2 |
6 | 50 | 3 |
7 | 90 | 3 |
8 | 80 | 3 |
sql
select t.* from user_test_result t where score = (select max(score) from user_test_result where open_id = t.open_id) group by t.open_id;
结果:
id | score | uid |
---|---|---|
1 | 100 | 1 |
5 | 100 | 2 |
7 | 90 | 3 |