I'm trying to make a simple discussion board and as a result I need topics titles with their authors and authors of topics last posts.
So far I have this
SELECT
`t`.`id`,
`t`.`title`,
`t`.`date` as theme_date,
`u`.`id` as user_id,
`u`.`username` as user_username,
COUNT(p.id) as count_posts,
SUBSTRING_INDEX(GROUP_CONCAT(`p`.`id` ORDER BY `p`.`id` DESC SEPARATOR "| " ), "| ", 1) as last_post_id,
SUBSTRING_INDEX(GROUP_CONCAT(`p`.`date` ORDER BY `p`.`id` DESC SEPARATOR "| " ), "| ", 1) as last_post_date,
IFNULL(MAX(p.date), t.date) AS pts
FROM
`topics` t
LEFT JOIN
`posts` p ON `p`.`topics_id` = `t`.`id`
LEFT JOIN
`users` u ON `u`.`id` = `t`.`user_id`
GROUP BY
`t`.`id`
ORDER BY
`pts` DESC
LIMIT 10
I got here 10 latest topics titles, their authors usernames and IDs, number of messages in each topic, last ID and date of post in each topic, and everything sorted by activity... basically everything. All I need is authors username of those last posts. I guess I have to make one left join with subquery but I'm kinda stuck. Can someone help me?