id    user     score     do_time  
1         a              1               1
2         a              3               1
3         a              3               3
4         b              0               1
5         b              4               4
6         b              4               1
需求是 查询每个用户的最好成绩,当score一样时,取dotime时间最小的记录

解决方案 »

  1.   

    分两步
    第一步 取相同用户,分数一样的时间最大
    create table test select user,score,max(do_time) from tb group by user,score;
    第二部取相同用户分数大的
    select * from test A where not exists (select 1 from test B where A.user=B.user and A.score<B.score);
      

  2.   


    select user,max(score)as score,min(do_time)as do_time
    from a
    group by `user`
      

  3.   

    select t.* from (select * from mytable order by score desc, do_time asc) t group by t.user
      

  4.   

    SELECT ID,USER,SCORE,DO_TIME FROM (SELECT ID,USER,SCORE,DO_TIME FROM SCORE ORDER BY SCORE DESC,DO_TIME ASC)  GROUP BY USER
    这个是最简单的写法。
      

  5.   

    select user, max(score),do_time from test_gyy 
    GROUP BY `user` 
     ORDER BY do_time desc