Another solution that works is to use all attributes implicitly in GROUP BY
So the next one will be the final request
SELECT users.*,
COUNT(deals.id) AS num_deals
FROM users, deals
WHERE deals.users_id = users.id
GROUP BY users.id, users.name, users.attrib1, ..., users.attribN
ORDER BY num_deals LIMIT 5;
If you use the framework as rails, you can easily implement it using the Model.column_names function.
source
share