例表(user_test_result)如下:
| id | uid | score |
|---|---|---|
| 1 | user1 | 80 |
| 2 | user1 | 70 |
| 3 | user2 | 100 |
| 4 | user1 | 80 |
SQL语句
select t.*
from `user_test_result` t
where score=(
select max(score)
from `user_test_result`
where `uid`= t.uid
)
group by t.uid
结果
| id | uid | score |
|---|---|---|
| 1 | user1 | 80 |
| 3 | user2 | 100 |
分析
select t.* 保证选出的是这条记录, group by t.uid 保证选出的是最靠上的一条
本文介绍了一个SQL查询技巧,用于从用户测试结果表中选择每个用户的最高分。通过使用子查询和分组,确保了结果的准确性和效率。
174万+





