Answer the question
In order to leave comments, you need to log in
How to cross-group output via GROUP BY in MYSQL?
Hello!
There is a request like:
SELECT `from_user`,`to_user`,`text`,`read_state` FROM
( SELECT `from_user`,`to_user`,`text`,`read_state`,`ts` FROM `gr_messages` WHERE `to_user` = '1' OR `from_user` = '1' ORDER BY `ts` DESC )
as inv GROUP BY `from_user` ORDER BY `ts` DESC LIMIT 10
Answer the question
In order to leave comments, you need to log in
You want the impossible. I think that you have a problem in the algorithm or logic, that's why the problem arose. Think again.
As for your query, this grouping will no longer work on MySQL 5.7 due to the stricter sql_mode rules.
There is no way for you to get the line that you drew, because text = Good) wrote not 3, but 1.
But if you really want to, then shift the desired grouping to the application, calculating who 1 was talking to. Get away from the database - save your time and DBMS resources.
Or, I already said - change the logic.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question