求助,怎样用SQL实现

来源:百度知道 编辑:UC知道 时间:2024/05/12 12:07:49
id date msgid
1 20070708 1
2 20070908 1
3 20070304 2
4 20070506 3

希望得到 MSGID ,按DATE排序,MSGID有重复的只显示最近时间的

即:

msgid date
3 20070304
4 20070506
1 20070908

一楼的是错误的,首先多个数据不能用=,id=,也不是最大的max(id),而应该是max(date).
所以应该是
select * from
(
select msgid , max(date) as date
from 表
group by msgid
)as T
order by date

select msgid,date from 表 where id=(select max(id),msgid from 表 group by msgid) order by date