问题描述
我有一列在某些行中包含空值.
I have a column which contains null values in some of the rows.
我想通过在sqlite中编写一条select语句来对列值求和.
I want to do sum of the column values by writing a select statement in sqlite.
如何编写该语句,以便将空值视为0.
How do I write the statement so that it treats null values as 0.
我当前的sqlite语句:从table1中选择总和(金额)由于返回null而给出错误.
My current sqlite statement: select sum(amount) from table1gives error as it returns null.
请帮助.
推荐答案
您可以使用ifnull(x,y)或coalesce(x,y,z,...)函数.它们每个都从左至右从参数列表返回第一个非空值. ifnull有两个参数,而coalcece至少有两个.
You can use ifnull(x,y) or coalesce(x,y,z,...) function. Each of them return the first non-null value from the parameter list from left to right. ifnull has exactly two parameter whereas coalesce has at least two.
从表1中选择总和(ifnull(amount,0))
select sum(ifnull(amount, 0)) from table1
这篇关于如何为空记录编写sqlite select语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!