MySQL查询:将整个表中的max(Date)写入每一行

时间:2012-10-29 12:34:08

标签: mysql sql date max

我需要为整个表找到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

1 个答案:

答案 0 :(得分:1)

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 ...