First of all, sorry for my bad English...
I have a problem with one of my MySQL queries. I have got two tables: messages and replies. The messages table contains a id, discussion_id, content and date field. The replies table contains a id, message_id, content and date field.
I want that for one particular discussion the latest ten messages and their replies are fetched. The messages needs to be sorted on the last reply, the replies needs to be sorted first first.
(Like Facebook)
I can do that like this:
SELECT * FROM messages WHERE discussion_id = 'test' ORDER BY date DESC;
And then use a PHP foreach loop to
SELECT * FROM replies WHERE message_id = 'test' ORDER BY date ASC;
But if I would use that I've 11 queries, which isn't ideal. I was thinking about a join or union, but if I would do that, the replies and messages are not separatable. The list is sorted by replies.date, but the replies and their parent message are not together anymore. I'm using this:
SELECT * FROM messages LEFT JOIN replies ON messages.id = replies.message_id WHERE discussion_id='4cc43b40586b6' ORDER BY replies.date DESC
Is it possible to make one query for this task?