大家好,我有个用户操作记录表,每天同一用户可能会有几条记录,现在我想只查询一天只有一条记录,注意是一天一条记录,第二天也有一条记录,第三天也有一条记录。表结构如下:id    username      optTime
1       a           2013-06-20 18:39:45
1       a           2013-06-20 18:49:45
1       a           2013-06-21 18:39:45  
1       b           2013-06-20 18:39:45
1       b           2013-06-21 18:39:45
1       b           2013-06-22 18:39:45
1       c           2013-06-20 18:39:45
1       c           2013-06-20 18:49:45
1       c           2013-06-21 18:39:45结果应该是这样:
id    username      optTime
1       a           2013-06-20 18:39:45
1       a           2013-06-20 18:49:45  //去掉
1       a           2013-06-21 18:39:45  
1       b           2013-06-20 18:39:45
1       b           2013-06-21 18:39:45
1       b           2013-06-22 18:39:45
1       c           2013-06-20 18:39:45
1       c           2013-06-20 18:49:45 //去掉
1       c           2013-06-21 18:39:45只去掉两条记录而已。mysql去重

解决方案 »

  1.   

    select *
    from 用户操作记录表 t
    where not exists (select 1 from 用户操作记录表 where username=t.username and optTime<t.optTime)
      

  2.   

    1 楼的in和exist  应该没多大区别吧:
      

  3.   

    select * from 用户操作记录表 t
     where not exists(select 1 from 用户操作记录表 where username=t.username and optTime<t.optTime and date(optTime)=date(t.optTime))
      

  4.   

    mysql里面的in效率非常差  用not exists
      

  5.   

    not exists比in和distinct都要快应该not exists里面查询到的就是很小的数据了,不像in和distinct查询很大的量,这就有区别了!