Answer the question
In order to leave comments, you need to log in
How to select latest records with unique key with IN condition in MySQL?
Good afternoon.
There are 2 tables: Dialog and Messages.
I need to select the dialogs I need, and then the last messages from them (as in Vkontakte).
I have an array with the IDs of the dialogs, but I can't select the latest posts using the IN clause, because if there are 500 dialogs, I'll have to load the last post for each one.
The specific problem is that if you use IN + LIMIT + ORDER BY DESC, and if the messages in one dialog are consecutive, then only the messages of one dialog will be loaded.
Are there any constructions like "Normal SELECT + SELECT DISTINCT"?
Or "SELECT `id`, UNIQUE('conversation')"?
How can I literally in a couple of queries select the latest records from the database with a unique key using the IN condition or similar?
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question