<pre class="c" name="code">
SELECT id, name, `date`
FROM (
SELECT *
FROM tableXX
ORDER BY `date` DESC
) AS a
GROUP BY name
不過我不太確定你的意思,沒有取date
的話,對name做group by,怎樣都對阿?你是不是稍微解釋一下你的需求?
我講一下完整的需求好了
這是客戶的報表,客戶報表沒有用所謂的分類,但名稱會重複,所以名稱變成了分類(不是我規劃的…)
所以某會員所有的報表,可能有的是
a
a
b
b
c
c
a
a
以分類來說就是
a
b
c
而date是publish date,所以不一定id越大等於pulish date越大,因此也不能用MAX(id)
所以我要在以name為group的架構下,抓出我想要得那筆資料所有對得欄位
其實這樣可以作到啦,不過要篩選的欄位不只一個,用一堆子查詢總覺得不是好事
<pre class="c" name="code">SELECT s1.s_name, (SELECT s2.s_id FROM tbl_sort s2 WHERE s2.s_name = s1.s_name ORDER BY s2.s_date DESC LIMIT 0, 1) AS newId
FROM tbl_sort s1
WHERE s1.m_id = 1
GROUP BY s1.s_name
ORDER BY s1.s_date DESC
我答案裡面的第一個select把date拿掉不就行了?
<pre class="c" name="code">
SELECT id, name
FROM (
SELECT *
FROM tableXX
ORDER BY `date` DESC
) AS a
GROUP BY name
WITH POPO AS(
SELECT ID,
, NAME
ROW_NUMBER() OVER (PARTITION BY NAME ORDER BY DATE DESC) RN
FROM MYTABLE
)
SELECT ID, NAME
FROM POPO
WHERE RN = 1