有一张表,里面有两列,格式如下 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 这两个语句取出来的数据不一样,为什么,那个可以帮啊
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 这两个语句取出来的数据不一样,为什么,那个可以帮啊
--or
select * from userphone a where not exists(select 1 as regTime from userphone where meid=a.meid and regTime>a.regTime)
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)
查询结果还是跟去重
select distinct meid from userphone
差了很多啊
--have a try
select meid,max([time]) from userphone
group by meid