I have a little problem – i would can get with this query topics posts count and last post id, but i can’t figure out, how to get right user id. I get first post (lowest id) user id but i want lastest post… I have tried adding “ORDER BY id DESC” but this will not help. Any ideas how to do it?
SELECT
COUNT(`id`) AS `count`,
MAX(`id`) AS `last_post_id`,
`topic_id`,
`user_id`
FROM `forum_posts`
WHERE `topic_id` IN (326, 207, 251)
GROUP BY `topic_id`
In your query
useridreturns the userid of any userid in that group, which will typically be the first one that appears in whatever index the optimizer happened to pick, but it does not have to be.Self-join to fetch the user_id from the row that has the id you are interested in: