![]() |
I need help with ORDER BY date
Hi,
I have a table of messages, the structure could probably be better but I have to work with it as it is. The table has these fields:- Code:
I need to list the original messages, ie `original_message_id` = 0, by date DESC, but I want the date to include the replies. So if a message was sent ages ago, but it just receives a reply I want it listed above messages sent a short time ago. I have no idea how to do this. If anyone can help I would greatly appreciate it. |
Hmmm...
Off the top of my head, untested: Code:
SELECT M1.id, M1.to_id, M1.from_id, M1.message, M1.`date` AS originalMessageDate, |
Quote:
I know I can filter the results as I retrieve them but would there some way to limit the return list to only show ones where original_messsage_id = 0? I only want to retrieve original messages, but in order of latest reply if there is one. Appreciate the help. |
Oh, sure...
Just add that as WHERE. Code:
SELECT M1.id, M1.to_id, M1.from_id, M1.message, M1.`date` AS originalMessageDate, |
Quote:
|
SELECT M1.id, M1.to_id, M1.from_id, M1.message, M1.`date` AS originalMessageDate,
MAX( IFNULL(M2.`date`,M1.`date`) )AS lastReplyDate FROM messages AS M1 LEFT JOIN messages AS M2 ON M1.id = M2.original_messsage_id GROUP BY M1.id, M1.to_id, M1.from_id, M1.message, M1.`date1 ORDER BY lastReplyDate DESC you may try this code..it may solve your problem covetus |
| All times are GMT +1. The time now is 11:00 AM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.