I have 2 tables:
Message:
id: INT
created_at: DATETIME
Comment:
id: INT
message_id: INT
created_at: DATETIME
One Message has many Comments.
I want to get all Messages ordered by the most recent activity:
- If
MessagehasComments, then the most recentComment‘screated_atis used as theMessageactivity indicator - If
Messagedoesn’t haveComments, it’screated_atvalue is the activity indicator
So basically, I want to sort it like a classic e-mail or private messaging system.
Maybe I can INNER JOIN the Comments, but I don’t think it’s necessary to get all the Comments just for the sort.
Also, I thought about creating a column in Message to save the last activity date and update it whenever a Comment is created, but I’d like to see if you have better solutions..
I’m using Doctrine, so if you have a Doctrine-based solution I’d rather that,
Thanks!
What I would do in MySQL is something like: