Answer the question
In order to leave comments, you need to log in
How to make a mysql select for a specific table?
Greetings!
There is such a table (Fig. 1) which is a chat between two interlocutors.
It is necessary to make a selection so that it would be possible to see the latest messages from each group of two interlocutors (Fig. 2)
The difficulty lies in the fact that user_id 0 and user_id 1 are one user_id.
rice. 1
fig. 2
Answer the question
In order to leave comments, you need to log in
Make a selection of the time of the last record for each pair of subscribers and attach to it the same table for subscribers and time.
Select o.* from (
Select max(updated_at) as maxdate, sender_user_id, recipient_user_id
from chattable
group by sender_user_id, recipient_user_id
) as t
join chattable as o on t.sender_user_id=o.sender_user_id and t.recipient_user_id=o.recipient_user_id and t.maxdate=o.upadet_at
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question