如何获得不重复的用户总数?

时间:2019-01-15 13:48:36

标签: mysql

我有表格付款,我需要使用户总数达到3个限额

SELECT SUM(money),username FROM `payments` ORDER BY money DESC LIMIT 3

表格:

+----------+--------+
| username | money  |
+----------+--------+
| Alex     | 200    |
+----------+--------+
| Alex     | 100    |
+----------+--------+
| John     | 50     |
+----------+--------+
| Emily    | 400    |
+----------+--------+
| etc ...

我如何获得此结果? :

+----------+--------+
| username | amount |
+----------+--------+
| Emily    | 400    |
+----------+--------+
| Alex     | 300    |
+----------+--------+
| John     | 50     |
+----------+--------+

1 个答案:

答案 0 :(得分:1)

因为您需要用户明智的总计,所以您必须添加group by子句

您可以在下面尝试-您需要添加group by clause

SELECT username, SUM(money) AS amount
FROM payments
GROUP BY username
ORDER BY amount DESC
LIMIT 3;