作者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