数据如下
Table  logid       user_id       weights        create_time
1        1                   402              2014-04-02 09:10:40
2        5                   1012            2014-04-02 15:23:52
3        2                   402              2014-04-08 08:57:32
4        1                   402              2014-04-08 09:53:39
想要得到的查询效果为:2        5                   1012            2014-04-02 15:23:52
4        1                   402              2014-04-08 09:53:39
请问sql语句如何写

解决方案 »

  1.   

    select  * from tt a where not exists(select 1 from tt where date(create_time)=date(create_time) and a.weights<weights
    or
    date(create_time)=date(create_time) and a.weights=weights and a.id<id
    )
      

  2.   

    tks 斑竹大人,可能我写得不够详细,给大家误会了 sorry 重新编辑下 需求
      

  3.   

    晕,现在都不能编辑主题了,重新写一下数据如下
    Table  log       user_id       weights        create_time
           1                   402              2014-04-02 09:10:40
           5                   1012            2014-04-02 15:23:52
           2                   402              2014-04-08 08:57:32
           1                   402              2014-04-08 09:53:39
           4                   402              2014-04-08 10:53:39
           1                   402              2014-04-08 11:53:39
           3                   5002            2014-04-11 11:53:39 
    想要得到的查询效果为:5                   1012            2014-04-02 15:23:52
    1                   402              2014-04-08 11:53:39
    3                   5002            2014-04-11 11:53:39 
      

  4.   

    select log.userid,log.weights, log.create_time from (
    select userid,max(create_time) max_create_time from log group by userid) tmp,log
    where tmp.userid = log.userid and tmp.max_create_time  = log.create_time order by log.create_time desc 
      

  5.   

    最后的desc 改成asc 或者去掉
      

  6.   

    tks benluobobo
      

  7.   


    -- 大概如下,日期函数,我也不大明白,按日期取最大的ID,再取这些ID的行就可以了。
    select * from mytable where id in (select max(id) from mytable group by date_format(create_time,'%Y-%m-%d'))
      

  8.   

    select *
    from log t
    where not exists (select 1 from log where date(create_time)=date(t.create_time) and weights>t.weights)
      

  9.   

    参考下贴中的多种方法http://blog.csdn.net/acmain_chm/article/details/4126306
    [征集]分组取最大N条记录方法征集,及散分....
      

  10.   

    select  * from tt a where not exists(select 1 from tt where date(create_time)=date(create_time) and a.weights<weights
     or
     date(create_time)=date(create_time) and a.weights=weights and a.create_time<create_time
     )