(select Max(score) from TABLE_NAME where subject=0)
union
(select Max(score) from TABLE_NAME where subject=1)
union
(select Max(score) from TABLE_NAME where subject=2)

解决方案 »

  1.   

    是不是需要在程序中可以对科目代号进行控制选择呢?
    否则的话使用max就可以了
    对了,使用集函数或者用union
      

  2.   

    哦,没看清,你是要查ID
    那应该是这样:
    select id from TABLE_NAME where score in(
    (select Max(score) from TABLE_NAME where subject=0)
    )
    union
    select id from TABLE_NAME where score in(
    (select Max(score) from TABLE_NAME where subject=1)
    )union
    select id from TABLE_NAME where score in(
    (select Max(score) from TABLE_NAME where subject=2)
    )
      

  3.   

    问题是现在不知道subject有几种。
      

  4.   

    select id,a.subject,max(score) as top 
    from
    (
    select max(score) as conscore,  subject
    from tablename
    group by suject
    ) b join tablename a
    on
    a.subject=b.subject
    and
    b.couscore=a.score
    group by id,a.subject我使用的是 sql server 2000,
    我在我的数据库上已经试过,完全可以!
      

  5.   

    那就再加上
    (
      select id from TABLE_NAME where score in(
        (select Max(score) from TABLE_NAME where subject=0)
      ) and subject=0
    )
    union
    (
    select id from TABLE_NAME where score in(
        (select Max(score) from TABLE_NAME where subject=1)
      ) and subject=1
    )
    union
    (
      select id from TABLE_NAME where score in(
        (select Max(score) from TABLE_NAME where subject=2)
      ) and subject=2
    )===================================================
    倒是,不知道SUBJECT有几种应该是个问题。
      

  6.   

    分个组不就完了吗select id from (select max(score) as 最高分 from 表 group by  subject) as 科目最高分表, TableName
    where TableName.score= 科目最高分表.最高分
      

  7.   

    tablename代表你的表名
    select id from (select max(score) as 最高分 from tablename group by  subject) as 科目最高分表, tablename
    where tablename.score= 科目最高分表.最高分
      

  8.   

    qazxsw1982103(c++) 你的做法存在着我前面所说的那种缺陷。
     landro你的方法我现在无法验证。明天试一下。
      

  9.   

    想了一下。 landro 的方法是可行的。
      

  10.   

    而且你这种方法有个问题。比如数学最高分是80,语文最高分是85,当然语文也有人是80分。那么你查数学最高分的人的时候,把语文是80分的那个学生也查进去了
    ------------------------------对,有这个缺陷那就这样select id from (select max(score) as 最高分,subject from tablename group by  subject) as 科目最高分表, tablename
    where  tablename.score= 科目最高分表.最高分 and tablename.subject=科目最高分表.subject
      

  11.   

    qazxsw1982103(c++) 最后的改进方法值得关注。
      

  12.   

    你这种方法应该是有语法上的错误,不能max(score)和subject一起select的。用group by子句的时候,所选择的列名必须在是在跟在group by后面的。在这里你只能select max(score), subject from tablename group by subject
      

  13.   

    没有阿
    我用sqlsever测试了的
      

  14.   

    select a.subject,a.id,a.score
    from aa as a,(select subject,max(score) as score
    from aa
    group by subject) as b
    where a.subject=b.subject and a.score=b.score
    order by a.subject ,a.id
      

  15.   

    验证过了。 landro和 qazxsw1982103(c++) 的方法均可行。
    1.
    select id, b.max_score, b.subject from (select max(score) as max_score, subject from scores group by subject ) b join 
    ( select id, score, subject from scores ) a 
    on b.max_score = a.score and b.subject= a.subject 2.
    select id, max_score, a.subject from (select max(score) as max_score, subject from scores group by subject) b, scores a
    where a.subject= b.subject and a.score= b.max_score