一套答题系统,每个人可以做多次,统计的时候只统计每个人的最高分数.tba表id   email     score
1    [email protected]   40
2    [email protected]     30
3    [email protected]     30
4    [email protected]     60
5    [email protected]   50
6    [email protected]     60
结果:id   email     score
4    [email protected]     60
5    [email protected]   50
6    [email protected]     60

解决方案 »

  1.   

    select * from t a where not exists
    (
    select 1 from t where email=a.email and id>a.id
    )
      

  2.   

    select email, max(score) score from t 
     group by email
      

  3.   

    select * from t a where not exists
    (
    select 1 from t where email=a.email and score>a.score
    )
      

  4.   

    --那里不对?
    create table t(id int,email varchar(20),score int)
    insert t select 1,'[email protected]',40
    union all select 2,'[email protected]',30
    union all select 3,'[email protected]',30
    union all select 4,'[email protected]',60
    union all select 5,'[email protected]',50
    union all select 6,'[email protected]',60
    select * from t a where not exists
    (
    select 1 from t where email=a.email and id>a.id
    )drop table tid          email                score       
    ----------- -------------------- ----------- 
    4           [email protected]               60
    5           [email protected]             50
    6           [email protected]               60
      

  5.   

    bill024(咖啡熊) ( ) 信誉:100    Blog   加为好友  2007-06-01 14:52:28  得分: 0  
     
     
       select * from t a where not exists
    (
    select 1 from t where email=a.email and id>a.id
    )
      
     
    你把咖啡熊的id>a.id换成score>a.score
      

  6.   

    select t1.* 
     from tba t1,
          (
            select email,max(score) as score
            from tba
            group by email
          ) T2
    where T1.email=t2.email
      

  7.   


    select a.id,b.email,b.score from t a
    left join
    (
    select email,max(score)as score from t 
    group by email
    )b a.score=b.score and a.email=b.email
      

  8.   

    刚有点错误create table tt(id smallint,   email varchar(10),     score smallint)
    insert into ttselect 1,    '[email protected]',   40 union all
    select 2,    '[email protected]'  ,   30 union all
    select 3,    '[email protected]'  ,   30 union all
    select 4,    '[email protected]'  ,   60 union all
    select 5,    '[email protected]',   50 union all
    select 6,    '[email protected]'  ,   60select a.id,b.email,b.score from tt a
     join
    (
    select email,max(score)as score from tt 
    group by email
    )b on a.score=b.score and a.email=b.email
    order by a.id
    id     email      score  
    ------ ---------- ------ 
    4      [email protected]     60
    5      [email protected]   50
    6      [email protected]     60(所影响的行数为 3 行)