一个sql问题

来源:百度知道 编辑:UC知道 时间:2024/06/15 02:14:32
ID SN TITLE USERID POSTTIME(datetime类型)
1 APPLE 标题3 用户a 2007-01-04
2 SUN 标题2 用户b 2007-01-03
3 SUN 标题1 用户b 2007-01-02
4 APPLE 标题4 用户a 2007-01-05

求每种SN的POSTTIME时间最晚的ID,SN,TITLE,USERID,POSTTIME
结果应为:
2 SUN 标题2 用户b 2007-01-03
4 APPLE 标题4 用户a 2007-01-05

我卡住了,怎么也做不出了。

select * from yourtable a where not exists(select 1 from yourtable where sn=a.sn and posttime>a.posttime)
效率比group by sn算出max time要高

select * from table_name
group by SN
having max(POSTTIME)

twhwj502你得sql语句能执行吗?
Joyice2717倒是可以考虑.

select * from tb inner join (select sn,max(posttime) posttime from tb group by sn) t on tb.sn=t.sn and tb.posttime=t.posttime

假设表名为try

select * from try where posttime in (select max(posttime) from try group by sn)