现有表 test   字段如下
calssID    userid    chengji    name     sex
  1          1         50       张3       男
  1          2         51       李四      男
  1          3         52       王五      男
  2          4         56       张31      男
  2          5         57       李四1     男
  2          6         58       王五1     男
  3          7         60       张32      男
  3          8         62       李四2     男
  3          9         63       王五2     男
现在要通过classID分组 也就是分成n组
查询出来每一组的成绩最高的学生name,sex,chengji,userid
谁能帮我写一下sql语句  立马给分!!!

解决方案 »

  1.   

    select * from test a where not exists(select * from test b where a.classId=b.classId and b.chengji>a.chengji)
      

  2.   


    create table test(
      calssID int,
      userid int,
      chengji int,
      name  varchar(10),
      sex  varchar(2)
    )
    insert into test
    select 1,1,50,'张3','男' union all
    select 1,2,51,'李四','男' union all
    select 1,3,52,'王五','男' union all
    select 2,4,56,'张31','男' union all
    select 2,5,57,'李四','男' union all
    select 2,6,58,'王五1','男' union all
    select 3,7,60,'张32','男' union all
    select 3,8,62,'李四2','男' union all
    select 3,9,63,'王五2','男'
     
    goselect t.* from test t where chengji=(select max(chengji) from test where calssID=t.calssID) order by t.calssIDdrop table test
      

  3.   

    select name ,sex,chengji,userid from test 
    group by classID 
    having chengji = max(chengji) ;
      

  4.   

    select name,sex,chengji,userid from test where chengji in (SELECT max(chengji) FROM test group by calssID) 
      

  5.   

    select name ,sex,chengji,userid from test 
    group by classID 
    having chengji = max(chengji) ;
    正解
      

  6.   

    要补习SQL 啊 可以说web开发 必须精通SQL
      

  7.   


    select * from test where chengji in (select max(chengji) from test group by calssID)
      

  8.   

    个人认为 yiyi_wx  的好一点
      

  9.   

    谢谢各位的支持
    我就是需要补习一下sql了
    呵呵!
    生命不息 学习不止
    谢谢各位!!!
    如果谁有更好的方法 希望继续讨论!
      

  10.   

    [align=right]
    select * from test t where t.chengji in
    (SELECT max(chengji) FROM test group by calssID)
    [/align]