让我们假设下表(例如几个内连接语句的结果):
id | column_1 | column_2
------------------------
1 | 1 |
2 | 2 | 2
3 | | 3
例如,您可以从以下语句中得到:
select a.id, t1.column_1, t2.column_2
from a
left join t1 on a.id = t1.id
left join t2 on a.id = t2.id
现在,如果我想将 t1.column_1 和 t2.column_2 总结如下
select
a.id,
t1.column_1,
t2.column_2,
(t1.column_1 + t2.column_2) as cumulated
from a
left join t1 on a.id = t1.id
left join t2 on a.id = t2.id
结果如下:
id | column_1 | column_2 | cumulated
------------------------------------
1 | 1 | NULL | NULL
2 | 2 | 2 | 4
3 | NULL | 3 | NULL
我的问题基本上是:有没有办法将 NULL 类型转换为 0 以便进行一些数学运算?
我努力了CONVERT(t1.column_1, SIGNED)
and CAST(t1.column_1 as SIGNED)
, but a NULL
保持NULL
.
Use IFNULL(column, 0)
将列值转换为零。
或者,COALESCE 函数将执行相同的操作:COALESCE(column, 0)
, except
-
COALESCE
符合 ANSI 标准,IFNULL
is not
-
COALESCE
接受任意数量的列/值,并将返回传递给它的第一个非空值。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)