Re: [SQL ] 顯示最新一筆資料問題
表格內容分析起來
好像可以如下寫法
也能達到你的需求:
select *
from 訂單 t1 inner join (
select id,max(date)
from 訂單
group by id
) t2 on t1.id and t2.id
where t1.date = t2.date
※ 引述《smallsafe (朋友都消失了)》之銘言:
: 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: 118.170.11.31
※ 編輯: cutekid 來自: 118.170.11.31 (10/17 23:25)
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):