sql 统计多个字段的和(如果字段中含有 null 的处理)
mysql 的ifnull 函数和 oracel 的 nvl 的函数相同功效 ;
ifnull(num,0) : 含义 就是如果num 为null 就返回0 ; 不是null 返回原值 ;
mysql:
SELECT sum(ifnull(num,0)+ifnull(nvl,0)) as _count from `cl_vul` where id =4 ;
mysql 的 COALESCE函数也有类似的效果
SELECT sum(coalesce(num,0)+coalesce(nvl,0)) as _count from `cl_vul` where id =4 ;
CAST : 强制转换函数
SELECT sum(CAST(num as UNSIGNED)+CAST(nvl as UNSIGNED)) as _count from `cl_vul` where id =4 ;
oracel :
SELECT sum(nvl(num,0)+nvl(nvl,0)) as _count from `cl_vul` where id =4 ;
注意 : nvl 的函数是属于oracel ,不是mysql 函数 ; 注意你的开发环境 ;
mysql环境使用nvl 报错
本作品采用《CC 协议》,转载必须注明作者和本文链接