0
I have a chat and would like to display the contacts according to the date of the last message, I thought of many ways to do this but it is not working in any way. How could I bring this data properly?
Structure of the Tables
The table message stores all messages, however, each message has the session_id.
The table Session has session information, ie messages are organized by session, within Session, has the contact ID.
The table contact has all contact information.
Goal
My idea is to give the SELECT in the contact table, but arrange them by the time of the last message.
For example, imagine a chat with your next-door contacts, you’re exchanging messages with some of them, but even if you’re exchanging messages, this contact is being displayed below some that you talked to yesterday.
The idea is this contact you’re talking to right now, climb to the top of your contacts.
Example
Below is an example of one of SELECTS that went wrong, it even returns in order, but with each message, the contacts are repeated.
SELECT *
FROM contact AS c
INNER JOIN message AS m
INNER JOIN session AS s ON s.contact_id = c.id AND m.session_id = s.id
ORDER BY m.timestamp;
The column of the time at which the message was sent is timestamp
If anyone can help, I’d appreciate it!
OBS: My forte is not database.
It would be interesting to put in your question what you tried so far, an example of the Sqls that you tried but went wrong.
– Matheus Ribeiro
Can put an example of the data and the desired output so we can test the solutions?
– Sorack
And the version of
MySQL
? What is?– Sorack