sql >> Base de Datos >  >> RDS >> Mysql

Usando SQL JOIN y COUNT

Si OP desea incluir información adicional (agregaciones adicionales, etc...) utilizando datos de users tabla:

SELECT `users`.`id`,
       count(`receipts`.`uId`)
FROM `users`
INNER JOIN `receipts` ON `users`.`id` = `receipts`.`uId`
GROUP BY `users`.`id`
ORDER BY count(`receipts`.`uId`) DESC

De lo contrario, solo los receipts la tabla es requerida...

SELECT `users`.`id`,
       count(`receipts`.`uId`)
FROM `receipts`
GROUP BY `receipts`.`uId`
ORDER BY count(`receipts`.`uId`) DESC