1

I'm trying to create an Inbox list, where only messages with distinct "parent ids" are displayed. So the parent id for a message from user1 to user2 will be the same as the parent id of a message from user2 to user1. Thus, my inbox will only display one item for every unique conversation. But I can't figure out how to select the distinct parent id and THEN the related message information. This is what I have now:

$sql = "SELECT sender, receiver, message, timestamp, (distinct parent)
    FROM messages WHERE receiver='$log_username' OR sender='$log_username' 
    ORDER BY timestamp DESC
    ";

I want to be able to grab the most recent message row from every unique parent group, and then put the parent groups with the most recent messages at the top of the inbox list. I can select distinct parent groups fine, but then I can't figure out how to also get the sender/receiver/message/timestamp data of the most recent message in that parent group.

0

1 Answer 1

1

This should give you what you are looking for. It will give you one message per parent and order by the most recently received message.

SELECT sender, receiver, message, timestamp, parent

FROM messages 

WHERE receiver='$log_username' OR sender='$log_username' 

GROUP BY parent

ORDER BY timestamp DESC
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.