I know there are similarly questions already asked like this one. But they don't seem to work for me.
What I am trying to do:
I am trying to get the last message of each contact of a given user.
Problem
Personal Message Table:
+-----------+---------------------+---------------------+-------------+
| sender_id | text | created_date | receiver_id |
+-----------+---------------------+---------------------+-------------+
| 16 | hello | 2020-11-22 22:37:40 | 15 |
| 15 | hi | 2020-11-22 22:37:55 | 16 |
| 15 | how are you ? | 2020-11-22 22:38:18 | 16 |
| 18 | hey | 2020-11-22 22:40:12 | 16 |
| 16 | you there ? | 2020-11-22 22:40:32 | 18 |
| 15 | where are you ? | 2020-11-22 23:50:23 | 18 |
| 15 | can we talk | 2020-11-22 23:50:38 | 18 |
| 18 | how is life ? | 2020-11-22 23:53:32 | 15 |
| 18 | whats up | 2020-11-22 23:54:38 | 15 |
+-----------+---------------------+---------------------+-------------+
With the above table we can see the user with id 15 has had conversation with user 16 and 18. What I want is the last message of each conversation so for conversation between user 15 and 16 the last message would be:
+-----------+---------------------+---------------------+-------------+
| sender_id | text | created_date | receiver_id |
+-----------+---------------------+---------------------+-------------+
| 15 | how are you ? | 2020-11-22 22:38:18 | 16 |
+-----------+---------------------+---------------------+-------------+
similarly last message of conversation between 15 and 18 will be:
+-----------+---------------------+---------------------+-------------+
| sender_id | text | created_date | receiver_id |
+-----------+---------------------+---------------------+-------------+
| 18 | whats up | 2020-11-22 23:54:38 | 15 |
+-----------+---------------------+---------------------+-------------+
My query so far:
select * from personal_message where receiver_id in
(
select receiver_id from personal_message where receiver_id in
(
select receiver_id from
personal_message where receiver_id = 15
) order by created_date desc
) group by sender_id;
I have tried many queries which I don't even remember.