I try few mysql statement but didn’t come to my expectations.
How to get the total of to_user chat and order by the lowest total?
Let say in this case,
id 7 chat with 2 user
id 6 chat with only 1 user.
so the minimum will be id 6.
Can someone help me with sql statement?
This is what my expected result
count | to_user |
---|---|
1 | 7 |
2 | 6 |
2
Answers
I think your problem will be solved with the following code:
Make sure you index the junction in both directions, otherwise it will BYITA later!
You may want to take into account the fact that the full list of chats for a given user is
(from any user, to given user) UNION (to any user, from given user)
.Consider user 1 in your sample data, who has sent messages to 5, 6, & 7 but not received any. And user 5 has sent to user 7 and received from user 1.
returns the following (which matches the expected result detailed in your question, errors aside)
whereas
returns