[請益] SQL抓取最新資料問題

看板PHP作者 (朋友都消失了)時間12年前 (2013/10/17 22:36), 編輯推噓1(105)
留言6則, 3人參與, 最新討論串1/1
我的資料為 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)

10/18 00:06, , 1F
select * from orders group by id order by date desc ?
10/18 00:06, 1F

10/18 00:12, , 2F
select a.* from [訂單] a inner join (
10/18 00:12, 2F

10/18 00:13, , 3F
select id, max(date) as date from 訂單 group by id
10/18 00:13, 3F

10/18 00:13, , 4F
) b on a.id=b.id and a.date=b.date order by a.id
10/18 00:13, 4F

10/20 21:26, , 5F
select id,str,max(date),mark from orders group by id
10/20 21:26, 5F

10/20 21:26, , 6F
這樣應該就行了
10/20 21:26, 6F
文章代碼(AID): #1IN_N_Kh (PHP)
文章代碼(AID): #1IN_N_Kh (PHP)