如题,比如: 
用户a,时间,其它字段 
用户b,时间,其它字段 
用户a,时间,其它字段 
用户c,时间,其它字段 
想取每个用户,每天时间最晚的那条记录。表中时间格式,年月日小时分秒都有。 
最后结果是 
用户a,时间,其它字段 
用户b,时间,其它字段 
用户c,时间,其它字段 这个问题我在oracle里面请教过,但那个语法在mysql里面执行不了。谁给个mysql里面的写法。谢谢!

解决方案 »

  1.   

    select *
    from yourTable a
    where not exists (select 1 from yourTable where 用户=a.用户 and 时间>a.时间)
      

  2.   

    http://topic.csdn.net/u/20091231/16/2f268740-391e-40f2-a15e-f243b2c925ab.html
     [征集]分组取最大N条记录方法征集,...
      

  3.   

    想取每个用户,每天时间最晚的那条记录select *
    from yourTable a
    where not exists (select 1 from yourTable where 用户=a.用户 and date(时间)=date(a.时间) and 时间>a.时间)
    建议提问时用例子来描述,这样可以避免理解上的偏差,反而需要更多的描述和沟通。
     (不要高估你的汉语表达能力或者我的汉语理解能力)
       建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
       参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
       
       1. 你的 create table xxx .. 语句
       2. 你的 insert into xxx ... 语句
       3. 结果是什么样,(并给以简单的算法描述)
       4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
       
       这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。   
      

  4.   

    抱歉,可能真的是我描述不清楚。
    表中两个字段,
    name_a,2009-12-01 11:10:20
    name_b,2009-12-01 12:11:20
    name_c,2009-12-01 20:10:20
    name_a,2009-12-07 11:10:20
    name_a,2009-12-07 19:10:20
    执行后结果
    name_a,2009-12-01 11:10:20
    name_b,2009-12-01 12:11:20
    name_c,2009-12-01 20:10:20
    name_a,2009-12-07 19:10:20
    如果第一个字段,在同一天有重复的,就把时间早的那条去掉,如果当天里没有重复就保留该记录
      

  5.   

    原表,
    name_a,2009-12-01 11:10:20 
    name_b,2009-12-01 12:11:20 
    name_c,2009-12-01 20:10:20 
    name_a,2009-12-07 11:10:20 
    name_a,2009-12-07 19:10:20 
    三楼的答案,执行结果是
    name_b,2009-12-01 12:11:20 
    name_c,2009-12-01 20:10:20 
    name_a,2009-12-07 19:10:20 
    而我想要的是
    name_a,2009-12-01 11:10:20 
    name_b,2009-12-01 12:11:20 
    name_c,2009-12-01 20:10:20 
    name_a,2009-12-07 19:10:20 
    如果第一个字段,在同一天有重复的,就把时间早的那条去掉,如果当天里没有重复就保留该记录
      

  6.   

    SELECT a.name,a.time from tt2 a left join tt2 b on (a.name=b.name)
    and 
    (date_format(a.time,'%Y-%m-%d)=date_format(b.time,'%Y-%m-%d) and a.time>=b.time
    )
    group by 
    a.name,a.time having count(b.time)=1
      

  7.   

    SELECT a.name,a.time from tt2 a left join tt2 b on (a.name=b.name)
    and 
    (date_format(a.time,'%Y-%m-%d)=date_format(b.time,'%Y-%m-%d) and a.time>=b.time
    )
    group by 
    a.name,a.time having count(b.time)=1
      

  8.   

    !!! #6楼 的试验是怎么做的?! 的确有些让人火冒! 也不肯提供你的create table , insert into 语句,别人的测试结果完全正确。 这样反而是浪费时间。
    mysql> select * from t_cashcow;
    +--------+---------------------+
    | name   | td                  |
    +--------+---------------------+
    | name_a | 2009-12-01 11:10:20 |
    | name_b | 2009-12-01 12:11:20 |
    | name_c | 2009-12-01 20:10:20 |
    | name_a | 2009-12-07 11:10:20 |
    | name_a | 2009-12-07 19:10:20 |
    +--------+---------------------+
    5 rows in set (0.00 sec)mysql> select *
        -> from t_cashcow a
        -> where not exists (select 1 from t_cashcow where name=a.name and date(td)=date(a.td) and td>a.td);
    +--------+---------------------+
    | name   | td                  |
    +--------+---------------------+
    | name_a | 2009-12-01 11:10:20 |
    | name_b | 2009-12-01 12:11:20 |
    | name_c | 2009-12-01 20:10:20 |
    | name_a | 2009-12-07 19:10:20 |
    +--------+---------------------+
    4 rows in set (0.06 sec)mysql>