如何在 MySQL 中查找一组列中值为空的记录
为此,使用 GREATEST() 概念。我们先创建一个表 −
mysql> create table DemoTable1862 ( Value1 int, Value2 int, Value3 int, Value4 int ); Query OK, 0 rows affected (0.00 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable1862 values(43,34,56,42); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1862 values(NULL,78,65,NULL); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1862 values(110,NULL,78,NULL); Query OK, 1 row affected (0.00 sec)
使用 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)
广告