如何使用MySQL在一组列中查找具有空值的记录
为此,请使用的概念GREATEST()
。让我们首先创建一个表-
mysql> create table DemoTable1862 ( Value1 int, Value2 int, Value3 int, Value4 int );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1862 values(43,34,56,42); mysql> insert into DemoTable1862 values(NULL,78,65,NULL); mysql> insert into DemoTable1862 values(110,NULL,78,NULL);
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1862;
这将产生以下输出-
+--------+--------+--------+--------+ | Value1 | Value2 | Value3 | Value4 | +--------+--------+--------+--------+ | 43 | 34 | 56 | 42 | | NULL | 78 | 65 | NULL | | 110 | NULL | 78 | NULL | +--------+--------+--------+--------+ 3 rows in set (0.00 sec)
这是在一组列中查找具有空值的记录的查询-
mysql> select * from DemoTable1862 where greatest(Value1,Value2,Value3,Value4) IS NULL;
这将产生以下输出-
+--------+--------+--------+--------+ | Value1 | Value2 | Value3 | Value4 | +--------+--------+--------+--------+ | NULL | 78 | 65 | NULL | | 110 | NULL | 78 | NULL | +--------+--------+--------+--------+ 2 rows in set (0.00 sec)