有一张表,里面有两列,格式如下 meid            time
58467874       2011-01-01 00:00:00
.....其中,meid 这一列有重复的,就是说,同一个meid 在表中可能对应多个时间,现在想通过语句查询,查询表中的全部内容,如果meid相同,则取time最大的那个(距现在最近的时间),语句该怎么写。
我用下面的语句查询
select * from userphone where regTime in(select MAX(regTime) as regTime from userphone group by meid)select distinct meid from userphone 这两个语句取出来的数据不一样,为什么,那个可以帮啊

解决方案 »

  1.   

    补充,就是希望取出来的数据中meid的值是唯一的
      

  2.   

    select * from userphone a where regTime in(select MAX(regTime) as regTime from userphone where meid=a.meid)
    --or
    select * from userphone a where not exists(select 1 as regTime from userphone where  meid=a.meid and regTime>a.regTime)
      

  3.   

    modify:
    select * from userphone a where regTime in(select MAX(regTime) from userphone where meid=a.meid)
    --or
    select * from userphone a where not exists(select 1 from userphone where  meid=a.meid and regTime>a.regTime)
      

  4.   

    select * from userphone t where regTime=(select MAX(regTime) as regTime from userphone where meid=t.meid)
      

  5.   


    查询结果还是跟去重
    select distinct meid from userphone 
    差了很多啊
      

  6.   

    补充一个,如果时间也相同了,我刚看了一下,有时间相同的,就是表中同一个meid 对应两个时间,切时间也相同
      

  7.   


    --have a try
    select meid,max([time]) from userphone
    group by meid