When I use Max to find the maximum value of a field in a particular MySQL Group after using GROUP BY, is it possible to get the entire row which contains the maximum value?
解决方案
I stumbled across this thread when working on some forum code. I wanted to get the latest post for each thread and display it in the list of threads for a particular board.
Quassnoi's answer above was very helpful to me and I was able to adapt it. Here is the code in case it helps anyone else:
SELECT p.id As post_id, p.post_number, p.topic_id
FROM forum_post p, (
SELECT topic_id, MAX(post_number) As max_post_number
FROM forum_post d
GROUP BY topic_id) dd
WHERE p.id =
(
SELECT id
FROM forum_post
WHERE topic_id = dd.topic_id
AND post_number = dd.max_post_number
ORDER BY topic_id DESC, post_number DESC, id DESC
LIMIT 1
)