我需要找到整个表的max(n.LastEdited)(最新编辑日期)并将其写在每一行上。
我有的:
SELECT n.ID,
n.Title,
n.News,
(SELECT max(n.LastEdited)) AS NewsLastEdited
FROM News AS n
ORDER BY n.ID DESC
LIMIT 0,20
我得到了什么(不显示ID,标题,新闻等):
NewsLastEdited
2012-10-25 10:54:24
2012-10-25 10:54:12
2012-10-25 10:54:02
我想要的是:
NewsLastEdited
2012-10-25 10:54:24
2012-10-25 10:54:24
2012-10-25 10:54:24
最佳答案
SELECT n.*
FROM news n
INNER JOIN
(
SELECT ID, Max(LastEdited) lastEdit
FROM news
GROUP BY ID
) x ON n.ID = x.ID AND
n.LastEdited = x.LastEdit
-- WHERE ...
-- ORDER BY ...
-- LIMIT ...