0

I'm trying to developed some sort of ticket system, but I'm having trouble with a small part.

So, we have discussions, stored in the "discussions" table which are order by UNIX timestamp column "created_at"

Then, I have "comments" table, which has the tickets responses.

What I want to do is order the tickets by the recent activity basically.

I'm not that good at joining tables in queries, this is what I tried but it doesn't work sadly: /

SELECT *
FROM (`discussions`)
JOIN `comments` ON `comments`.`object_id` = `discussions`.`id`
WHERE `project_id` = '2'
  AND `comments`.`type` = 'discussion'
ORDER BY `comments`.`created_at` ASC,
         `discussions`.`created_at` DESC

I'm using CodeIgniter and this is the Active Record code

$query = $this->db->where('project_id', $id)
                        ->join('comments', 'comments.object_id = discussions.id')
                        ->where('comments.type', 'discussion')
                        ->order_by('comments.created_at', 'asc')
                        ->order_by('discussions.created_at', 'desc')
                        ->get('discussions');

Thanks a lot for the help!

4

1 回答 1

0

试试这个查询

SELECT * FROM discussions d, comments c
where c.object_id = d.id
and project_id = '2' 
AND c.type = 'discussion' 
ORDER BY c.created_at,d.created_at desc
于 2013-04-17T14:06:24.797 回答