[SQL ] 顯示最新一筆資料問題

看板Database作者 (朋友都消失了)時間12年前 (2013/10/17 22:39), 編輯推噓1(104)
留言5則, 2人參與, 最新討論串1/2 (看更多)
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

10/18 09:13, , 1F
select * from 訂單 o
10/18 09:13, 1F

10/18 09:13, , 2F
where date = (select max(date)
10/18 09:13, 2F

10/18 09:14, , 3F
from 訂單 where date = o.date)
10/18 09:14, 3F

10/24 16:27, , 4F
WITH a AS (SELECT *,max(date) OVER (PARTITION BY id) n
10/24 16:27, 4F

10/24 16:28, , 5F
FROM 訂單) SELECT * FROM a WHERE date=n
10/24 16:28, 5F
文章代碼(AID): #1IN_QVHL (Database)
文章代碼(AID): #1IN_QVHL (Database)