本帖最后由 ldlovemm 于 2012-03-02 10:19:27 编辑

解决方案 »

  1.   

    select max(addtime),distinct(deviceid)
    from tab_tmp
    group by deviceid;
      

  2.   

    感谢楼上的回答,不过没有达到我要的效果,可能我没有把问题描述清楚。我想要的是:
     表(暂且叫 tab_tmp)有80万条记录,其中有两个字段是 'deviceid'(设备ID)以及 'addtime'(记录添加时间),现在想在这张表里面查询所有不重复 'deviceid' 的记录,且这条记录(包括其他字段)的 ‘addtime’ 是最大的。
      

  3.   

    select a.* from tab_tmp as a inner join
      (  
        select deviceid, count(*) as num
         from tab_tmp
        group by deviceid having num=1
      ) b
    on a.deviceid=b.deviceid;
      

  4.   

    select * from tab_tmp as a
    where not exists(select * from tab_tmp 
                      where a.devceid=devceid and addtime>a.addtime)