为此,您可以使用IFNULL()
NULL和NON-NULL值进行数学计算。让我们首先创建一个表-
mysql> create table DemoTable1462 -> ( -> Value1 int, -> Value2 int -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1462 values(10,20); mysql> insert into DemoTable1462 values(50,NULL); mysql> insert into DemoTable1462 values(NULL,70);
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1462;
这将产生以下输出-
+--------+--------+ | Value1 | Value2 | +--------+--------+ | 10 | 20 | | 50 | NULL | | NULL | 70 | +--------+--------+ 3 rows in set (0.00 sec)
以下是在具有NULL和NON-NULL值的MySQL表中执行数学计算的查询-
mysql> select ifnull(Value1,0)+ifnull(Value2,0) from DemoTable1462;
这将产生以下输出-
+-----------------------------------+ | ifnull(Value1,0)+ifnull(Value2,0) | +-----------------------------------+ | 30 | | 50 | | 70 | +-----------------------------------+ 3 rows in set (0.00 sec)