There are 2 tables
users, likes I make a request
SELECT u.role, COUNT( u.id ), COUNT( l.id ) FROM users u LEFT JOIN ( SELECT user_id, id FROM likes GROUP BY user_id ) AS l ON u.id = l.user_id GROUP BY u.role ORDER BY COUNT( u.id ) DESC I get the result format
| role | COUNT( u.id ) | COUNT( l.id ) | |--------------------------------------| | 1 | 50 | 10 | |--------------------------------------| | 2 | 40 | 1 | where the number of l.id is not calculated correctly
i.e., you need to get the number of users by roles and the number of likes submitted by users related to this role.
SELECT user_id, id FROM likes GROUP BY user_idis an invalid query. Either theidshould be in the aggregation function, or there should not be aGROUP BY. Think about what you wanted to achieve with this subquery. - Regent