I'm trying to recreate a conversation list like whatsapp where it displays the contact name of the person you are having a conversation with and also the time the message was posted and also the last message regardless of if it was from you or the contact that posted the message, In other words I need to always see the contact name and the last time the message was posted and the last message which could have been sent by me or the recipient, I also need to bring back the image of the other user from the users table.
USERS TABLE
userid | first_name | url |
---------------------------------------------
101 | name1 | www.image_url1.jpg |
102 | name2 | www.image_url2.jpg |
103 | name3 | www.image_url3.jpg |
104 | name4 | www.image_url4.jpg |
MESSAGES TABLE
MessageId | userid | senderid | message | timestamp |
-----------------------------------------------------------
1 | 101 | 102 | message1 | 1234567 |
2 | 102 | 101 | message2 | 1234578 |
3 | 101 | 102 | message3 | 1235679 |
4 | 103 | 101 | message4 | 1256379 |
What i'm trying to query from the database if my userid = 101 is the last message and timestamp from either me or the contact along with the contacts userid, first_name and url
userid | first_name | url | message | timestamp |
---------------------------------------------------------------------
102 | name2 | www.image_url2.jpg | message3 | 1235679 |
104 | name4 | www.image_url4.jpg | message4 | 1227878 |
----------------------------------------------------------------------
1) I want to extract the userid, first_name and url of the contact from the users table
2) And extract the newest message and timestamp from the messages table from the contact
Any help would be great...