I have a topic and topic_comments table. I want to join them both.
+------------+ +------------+ | Topic | | Comments | +------------+ +------------+ | id | | parent_id | | title | | content | | createdate | | createdate | | content | | creator_id | +------------+ +------------+
The join is on topic.id = topic_comments.parent_id
. I want to show the topic with latest comment and order by latest comment createdate
. And not show duplicate topics. Can anyone help me?
So far I have this:
select p.id, p.title, p.createdate, p.content, p.int_0 as reacties_total, p.char_1 as prio, p.char_0 as status, r.createdate as r_createdate, r.creator_id as r_creator_id, r.content as r_content from pages p, topic_reacties r where r.parent_id = p.id and p.parent_id = ' . $this->id . ' order by p.int_2 desc
This however doesn ‘t show topics without comments. It only returns topics with reactions.
Advertisement
Answer
SELECT title,content,max(createdate) as createdate FROM topic left join comments on topic.id=comments.parent_id group by title order by createdate desc;