注意是mysql!数据:
id score
9 500
3 495
3 492
3 491
6 490
7 480
2 340想得到的结果
id score
9 500
3 495
6 490就是找到 score 最大的前3条, 但是id不能重复, 我的问题出在老是把 3 491 找出来, 因此出现了 9 6 3 的结果
希望大家指教
id score
9 500
3 495
3 492
3 491
6 490
7 480
2 340想得到的结果
id score
9 500
3 495
6 490就是找到 score 最大的前3条, 但是id不能重复, 我的问题出在老是把 3 491 找出来, 因此出现了 9 6 3 的结果
希望大家指教
(select 1 from tb where id=a.id and score >a.score) limit 3;
+------+-------+
| id | score |
+------+-------+
| 9 | 500 |
| 3 | 495 |
| 6 | 490 |
+------+-------+
3 rows in set (0.00 sec)
(select 1 from tb where id=a.id and score >a.score) order by score desc limit 3;
+------+------------+
| id | max(score) |
+------+------------+
| 9 | 500 |
| 3 | 495 |
| 6 | 490 |
+------+------------+
3 rows in set (0.00 sec)
(select 1 from tb where id=a.id and score >a.score) order by score desc limit 3;第2种比较好理解