Hello everyone, in general there is such a problem, very interesting.

I have a user entity that is connected to many-to-one with itself (likes are like that). The question is how do I select a table of users by sorting by likes?

Here, for example, if I like the other in the user_likes table, the id , from_user_id , to_user_id is to_user_id - in general, if there is such a string, then you need to sort it and put it up. And yes when issuing costs LIMIT 10 .

    1 answer 1

    You can do the following:

     SELECT u.name AS name, COUNT(l.id) AS likes FROM users AS u LEFT JOIN users_likes AS l ON u.id = l.to_user_id GROUP BY u.name ORDER BY COUNT(l.id) DESC LIMIT 10