I have created a messaging system for users, it allows them to send a message to another user. If it is the first time they have spoken then a new conversation is initiated,
The simpleste way I think for this is:
Tables:
conversation(cid | userId | friendId | last_message_id)
messages(mid | message | userId | read | time | cid)
Then update last_message_id after each message insert by users in particulate conversation.
And then run this simple query. It will give you what you want.
SELECT * FROM conversation c, messages m
WHERE (c.userId='$uid' OR c.friendId='$uid')
AND c.last_msg_id=m.message_id
ORDER BY created_time DESC
$uid is id of logged in user.
So in actual what this process is doing: