There is a table of messages in the database. The structure is as follows:
CREATE TABLE `message` ( `ID_message` bigint(20) NOT NULL auto_increment, `ID_user` bigint(20) NOT NULL, `ID_user_to` bigint(20) NOT NULL, `created_at` datetime NOT NULL, `is_read_at` datetime default NULL, `text` text, PRIMARY KEY (`ID_message`) )
It is required to receive for the current user all last correspondence. Let the Id of the user who entered “My Messages” = 5. There are 5 entries in the Database with the following id:
ID_user = 5 ID_user_to = 2 created_at = 01.01.2000
ID_user = 5 ID_user_to = 2 created_at = 02.01.2000
ID_user = 3 ID_user_to = 5 created_at = 01/01/2000
ID_user = 2 ID_user_to = 5 created_at = 01/03/2000
ID_user = 5 ID_user_to = 3 created_at = 1/7/2000
So the user should see two lines:
Correspondence with user ID = 3 Last message: 1/7/2000, message text
Correspondence with user ID = 2 Last Post: 1/3/2000, message text
I made a request:
SELECT ID_user,ID_user_to, MAX(created_at) FROM `message` WHERE ID_user = 5 OR ID_user_to = 5 GROUP BY ID_user,ID_user_to ORDER BY created_at DESC
In this example, it will return the strings:
ID_user = 5 ID_user_to = 3 created_at = 1/7/2000
ID_user = 2 ID_user_to = 5 created_at = 01/03/2000
ID_user = 5 ID_user_to = 2 created_at = 02.01.2000
ID_user = 3 ID_user_to = 5 created_at = 01/01/2000
How to make it so that instead it displays:
ID_user = 5 ID_user_to = 3 created_at = 1/7/2000
ID_user = 2 ID_user_to = 5 created_at = 01/03/2000?
After all, messages from id = 5 for id = 2 and from id = 2 for id = 5 refer to the same correspondence.