作者smallsafe (朋友都消失了)
看板Database
標題[SQL ] 險是最新一筆資料問題
時間Thu Oct 17 22:39:24 2013
database:
id num date mark
-- ----- -------- ------------
1 a0001 9/1 訂單內容1
1 a0002 9/15 訂單內容2
2 a0003 9/17 訂單內容3
1 a0004 9/21 訂單內容4
3 a0005 9/24 訂單內容5
2 a0006 9/24 訂單內容6
1 a0007 10/1 訂單內容7
我想要每個id 都列出data最新的一筆資料,列出結果如下
id num date mark
-- ----- -------- ------------
1 a0007 10/1 訂單內容7
2 a0006 9/24 訂單內容6
3 a0005 9/24 訂單內容5
我寫法是
select * from 訂單
where date = (select max(date) from 訂單)
group by id, num, mark
但顯示出來卻不是我想要的,請問該怎麼寫才能列出以上的格式呢?
PS:我是用mySQL,不能用top指定的樣子
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 123.204.159.1
推 TeemingVoid:select * from 訂單 o 10/18 09:13
→ TeemingVoid:where date = (select max(date) 10/18 09:13
→ TeemingVoid:from 訂單 where date = o.date) 10/18 09:14
→ knstt:WITH a AS (SELECT *,max(date) OVER (PARTITION BY id) n 10/24 16:27
→ knstt:FROM 訂單) SELECT * FROM a WHERE date=n 10/24 16:28