gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 07:25:16 26 4
gpt4 key购买 nike

我有表付款,我需要获得总用户数限制为 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 |
+----------+--------+

最佳答案

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

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

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

关于mysql - 如何获得不重复的用户总数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54200200/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com