作者smallsafe (朋友都消失了)
看板PHP
标题[请益] SQL抓取最新资料问题
时间Thu Oct 17 22:36:44 2013
我的资料为
id Str date mark
-- ----- -------- ------------
1 a 9/1 订单内容1
1 b 9/15 订单内容2
2 c 9/17 订单内容3
1 d 9/21 订单内容4
3 e 9/24 订单内容5
2 f 9/24 订单内容6
1 g 10/1 订单内容7
我想要每个id 都列出data最新的一笔资料,列出结果如下
id Str date mark
-- ----- -------- ------------
1 g 10/1 订单内容7
2 f 9/24 订单内容6
3 e 9/24 订单内容5
我写法是
select * from 订单
where date = (select max(date) from 订单)
group by id, Str, mark
但显示出来却不是我想要的,请问该怎麽写才能列出以上的格式呢?
PS:我是用mySQL,不能用top指定的样子
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 123.204.159.1
※ 编辑: smallsafe 来自: 123.204.159.1 (10/17 23:23)
※ 编辑: smallsafe 来自: 123.204.159.1 (10/17 23:23)
1F:→ kerash:select * from orders group by id order by date desc ? 10/18 00:06
2F:推 lvlightvivi:select a.* from [订单] a inner join ( 10/18 00:12
3F:→ lvlightvivi:select id, max(date) as date from 订单 group by id 10/18 00:13
4F:→ lvlightvivi:) b on a.id=b.id and a.date=b.date order by a.id 10/18 00:13
5F:→ ej04cj86:select id,str,max(date),mark from orders group by id 10/20 21:26
6F:→ ej04cj86:这样应该就行了 10/20 21:26