我想要做的是选择(Money + Bank-Money)的总和并显示前10名(Money + Bank-Money)=玩家表中的总余额
我用来获取数据但不起作用的查询
SELECT `id`,`Username`, SUM(`Money`+`Bank-money`)
AS total-balance
FROM `players`
ORDER BY total-balance DESC LIMIT 0,10"
我该怎么办?
感谢
答案 0 :(得分:4)
SUM()
用于添加不同行的值。如果您想要一行中不同列的总和,只需添加它们:
SELECT `id`,`Username`, `Money` + `Bank-money` AS total-balance
FROM `players`
ORDER BY total-balance DESC LIMIT 10
答案 1 :(得分:2)
您需要使用GROUP BY
和COALESCE
(处理NULL值):
SELECT `Username`,
SUM(COALESCE(`Money`,0)+COALESCE(`Bank-money`,0)) AS `total-balance`
FROM `players`
GROUP BY `Username`
ORDER BY `total-balance` DESC
LIMIT 0,10