M
M
Morozken2018-05-15 20:07:56
MySQL
Morozken, 2018-05-15 20:07:56

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.
5afb12dfdf4e4626629531.png
rice. 1
5afb1301f1a87629216975.png
fig. 2

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2018-05-15
@Rsa97

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.

V
Vasya, 2018-05-16
@haramba

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

Instead of updated_at, you can use created_at or id
to filter users 0 and 1 more easily in a client script, php or other, or by an additional request for this selection

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question