MySQL Mysql查询求和十进制数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7682502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mysql query to sum decimal numbers
提问by Muhammad Imran Tariq
I am trying to sum my decimal records e.g.
我正在尝试对我的十进制记录求和,例如
There is a column in my table named cash having values like 40.33, 30.00, 77.22. How can I create a query that sum up these records? I am trying hard using SUM(cash)
and ROUND(SUM(cash),2)
but no result.
我的表中有一个名为 cash 的列,其值类似于 40.33、30.00、77.22。如何创建汇总这些记录的查询?我正在努力使用SUM(cash)
,ROUND(SUM(cash),2)
但没有结果。
SELECT cash FROM `enrolled
return rows.
返回行。
SELECT ROUND(SUM(cash),2)`
return 0 row.
返回 0 行。
回答by Nathan
try this.
尝试这个。
SELECT ROUND(SUM(cash), 2)
FROM <tablename>
If you are getting no results, then there must be a null value, try this instead.
如果你没有得到任何结果,那么一定有一个空值,试试这个。
SELECT ROUND(SUM(cash), 2)
FROM<tablename> a
WHERE cash IS NOT NULL
Tell me if there is anything else to help you with.
告诉我是否还有其他事情可以帮助您。
回答by mellamokb
You may have NULL values in the table causing issues. Try:
表中可能有 NULL 值导致问题。尝试:
SELECT SUM(COALESCE(`cash`, 0.00))
FROM MyTable
Should work just fine. Demo: http://sqlize.com/0fZI6J0Oi5
应该工作得很好。演示:http: //sqlize.com/0fZI6J0Oi5
回答by Ben
I would think
我会想
SELECT SUM(`cash`) FROM table_name
would do it
会做的
can you update your post to include the exact query you are using and
您可以更新您的帖子以包含您正在使用的确切查询吗?
DESCRIBE table_name