I have posts table. Each post has topic_id field which corresponds to the appropriate id field in topics table.
I need to sort topics by the date_created field in posts table.
Also, each topic has forum_id and I need to show and sort only topics with particular forum_id.
I tried something like this:
SELECT
topics.id, topics.title
FROM
topics RIGHT JOIN posts ON topics.id = posts.topic_id
WHERE
topics.forum_id = 1
ORDER BY
posts.date_created DESC
However not every topic has posts associated with it. Those that do not have any posts are not returned.
How to fix it?
Try using LEFT JOIN instead of RIGHT JOIN