简体   繁体   中英

MySQL Query: Write max(Date) from whole table to each row

I need to find the max(n.LastEdited) (Latest Editing Date) for my whole table and write it on each row.

What I have:

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

What I get (not showing ID, Title, News, ...):

NewsLastEdited
2012-10-25 10:54:24
2012-10-25 10:54:12
2012-10-25 10:54:02

What I want:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM