表a
id(varchar 主键) userid content time
asdsd 0001 aaa 2010-1-1
asddff 0002 dfdf 2010-1-2
sdsdd 0003 asdsd 2010-1-4
dfdffff 0002 fdgfdg 2010-1-4
要求用sql语句查询,每一个人所发表的最新言论,即按时间排序,如果userid有重复的才只显示最新的那条,id,userid都是没有规律的字符型,所以用max()不合适,请高手赐教用sql语句怎么写,我以往都是在程序里筛选的?
下面2位的答案都不正确
select m.* from a right join
(select UserID,max(time) as time from a group by UserID) v
on a.UserID=v.UserID and a.time=v.time
SELECT ta.*
FROM table_1 AS ta
WHERE id IN
(SELECT top 1 id
FROM table_1 AS tb
WHERE ta.userId=tb.userid and ta.time>=tb.Time)
ORDER BY time desc
select t.userid from T t where ... group by t.userid order by t.time desc
取结果集第一条记录,就是最新记录