id   name   type   score
1    ming   中学生  50
2    wang   大学生  80
3    lilei  小学生  70
4    qiang  小学生  60
5    mimi   中学生  66
6    coco   大学生  55
7    alex   中学生  44
8    bruce  大学生  88
9    tom    小学生  91
......
想得出每个学生类型(type)下成绩(score)排前2条的记录
谢谢。。大概很难

解决方案 »

  1.   

    本帖最后由 ACMAIN_CHM 于 2009-12-09 20:35:55 编辑
      

  2.   

    mysql> select * from t_JerryBeckF;
    +------+-------+--------+-------+
    | id   | name  | type   | score |
    +------+-------+--------+-------+
    |    1 | ming  | 中学生 |    50 |
    |    2 | wang  | 大学生 |    80 |
    |    3 | lilei | 小学生 |    70 |
    |    4 | qiang | 小学生 |    60 |
    |    5 | mimi  | 中学生 |    66 |
    |    6 | coco  | 大学生 |    55 |
    |    7 | alex  | 中学生 |    44 |
    |    8 | bruce | 大学生 |    88 |
    |    9 | tom   | 小学生 |    91 |
    +------+-------+--------+-------+
    9 rows in set (0.00 sec)mysql> select *
        -> from t_JerryBeckF a
        -> where 2>(select count(*)
        ->  from t_JerryBeckF
        ->  where type=a.type
        ->  and score>a.score)
        -> order by type,score desc;
    +------+-------+--------+-------+
    | id   | name  | type   | score |
    +------+-------+--------+-------+
    |    9 | tom   | 小学生 |    91 |
    |    3 | lilei | 小学生 |    70 |
    |    5 | mimi  | 中学生 |    66 |
    |    1 | ming  | 中学生 |    50 |
    |    8 | bruce | 大学生 |    88 |
    |    2 | wang  | 大学生 |    80 |
    +------+-------+--------+-------+
    6 rows in set (0.00 sec)mysql>
      

  3.   

    同名如何处理
    select a.name,a.type,a.score from tt a left join tt b
    on a.type=b.type and a.score<=b.score group by a.name,a.type,a.score having count(b.score)<=2