I have a database where I keep mail conversations
table "conversations"
ID SUBJECT
1 meeting on Friday
table "conversations_mails"
ID CONVERSATION_ID TEXT CREATED_ON
1 1 "What about a meeting on Friday?" 2012-08-05 10:00:00
2 1 "that's a good idea!" 2012-08-10 15:00:00
Now, i want to display a conversation overview page, showing a truncated text version of the latest reply. eg
"Meeting on Friday"
That's a good ...
I try to achieve this via GROUP BY. But what I get is the first reply in the table (“What about a meeting on Friday”), instead of the last “That’s a good Idea”.
This is my SQL statement:
SELECT *, MAX(conversations_mails.created_on) As conversation_last_reply,
MAX(conversations_mails.id) AS maxId
FROM conversations
LEFT JOIN conversations_mails ON conversations_mails.conversation_id = conversations.id
GROUP BY conversations.id
ORDER BY conversation_last_reply DESC
I know how to get the highest ID via MAX(), but what about the corresponding TEXT entry?
Thank you!
Try this-