Answer the question
In order to leave comments, you need to log in
How to display two tops on different dates?
I have a query that displays the top users by the amount for 24 hours, how can I display it for the month as well? That it was in one request.
SELECT `id_user`, SUM(`sum`) `sum`, users.name FROM `balance_history` INNER JOIN `users` ON `id_user` = users.id WHERE balance_history.date >= NOW() - INTERVAL 1 DAY AND `type` = 'plus' GROUP BY `id_user` ORDER BY `sum` DESC LIMIT 5
Answer the question
In order to leave comments, you need to log in
For example like this:
SELECT
`id_user`,
SUM(IF(`balance_history`.`date` >= NOW() - INTERVAL 1 DAY, `sum`, 0)) `sum_24h`,
SUM(`sum`) `sum_1mnth`,
`users`.`name`
FROM `balance_history`
INNER JOIN `users` ON `id_user` = users.id
WHERE `balance_history`.`date` >= NOW() - INTERVAL 1 MONTH AND `type` = 'plus'
GROUP BY `id_user`
ORDER BY `sum` DESC
LIMIT 5;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question