注意是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 的结果
希望大家指教

解决方案 »

  1.   

    mysql> select * from tb a where not exists 
    (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)
      

  2.   

    mysql> select * from tb a where not exists 
    (select 1 from tb where id=a.id and score >a.score) order by score desc limit 3;
      

  3.   

    mysql> select id,max(score) from tb group by id order by max(score) desc limit 3;
    +------+------------+
    | id   | max(score) |
    +------+------------+
    |    9 |        500 |
    |    3 |        495 |
    |    6 |        490 |
    +------+------------+
    3 rows in set (0.00 sec)
      

  4.   

    o yeah! 遇到高手了能烦请解释一下思路 和 select 1 是什么吗?分都是你的 :)
      

  5.   

    解释一下 select * from tb a where not exists 
    (select 1 from tb where id=a.id and score >a.score) order by score desc limit 3;第2种比较好理解
      

  6.   

    select id,max(score) from 数据 group by id order by 2 desc limit 3;