Reputation: 35
I have the table payments, I need get total amount users with a limit 3
SELECT SUM(money),username FROM `payments` ORDER BY money DESC LIMIT 3
Table:
+----------+--------+
| username | money |
+----------+--------+
| Alex | 200 |
+----------+--------+
| Alex | 100 |
+----------+--------+
| John | 50 |
+----------+--------+
| Emily | 400 |
+----------+--------+
| etc ...
How i can get this result? :
+----------+--------+
| username | amount |
+----------+--------+
| Emily | 400 |
+----------+--------+
| Alex | 300 |
+----------+--------+
| John | 50 |
+----------+--------+
Upvotes: 1
Views: 40
Reputation: 37483
as you need user wise total so, you've to add group by clause
You can try below - you need to add group by clause
SELECT username, SUM(money) AS amount
FROM payments
GROUP BY username
ORDER BY amount DESC
LIMIT 3;
Upvotes: 1