E
E
Egor Peregudov2014-02-02 07:33:42
MySQL
Egor Peregudov, 2014-02-02 07:33:42

How to return a list of dialogs based on messages?

-929206895.png
There is a table with user messages, you need to select a list of dialogs with an SQL query. In fact, you need to get 1 row with each user (user_id), sorted by date.
The screenshot shows the lines that should be in the answer.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Goryachev, 2014-02-02
@Stalk-p

Not the most beautiful and very difficult solution, it will slow down on large tables.

SELECT id, user_id, read_state, text, date, out
FROM Table AS T1
WHERE date = ( SELECT MAX(date) FROM Table Where user_id=T1.user_id )

or
SELECT T1.id, T1.user_id, T1.text, T1.date
FROM Table1 AS T1 INNER JOIN
( SELECT user_id, MAX(date) AS date FROM Table1 GROUP BY user_id) AS T2
ON T1.user_id=T2.user_id AND T1.date=T2.date

there is a FAQ on this topic
www.sql.ru/forum/687908/faq-vyborka-pervoy-posledn...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question