如何查找 MySQL 表中 EMPTY 或 NULL 列的数量?
我们首先创建一个表 -
mysql> create table DemoTable781 ( Name varchar(100) ); Query OK, 0 rows affected (0.66 sec)
使用 insert 命令在表中插入一些记录 -
mysql> insert into DemoTable781 values(''); Query OK, 1 row affected (0.29 sec) mysql> insert into DemoTable781 values('Chris'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable781 values(''); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable781 values(null); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable781 values(null); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable781 values(''); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable781 values(null); Query OK, 1 row affected (0.28 sec)
使用 select 语句显示表中的所有记录 -
mysql> select *from DemoTable781;
这将产生以下输出 -
+-------+ | Name | +-------+ | | | Chris | | | | NULL | | NULL | | | | NULL | +-------+ 7 rows in set (0.00 sec)
以下是查找 MySQL 表中 EMPTY 或 NULL 列数量的查询 -
mysql> (select SUM(CASE when Name IS NULL THEN 1 ELSE 0 END) AS NullCountAndEmptyCount from DemoTable781) UNION ALL (select SUM(CASE when Name='' THEN 1 ELSE 0 END) AS NullCountAndEmptyCount from DemoTable781);
这将产生以下输出 -
+------------------------+ | NullCountAndEmptyCount | +------------------------+ | 3 | | 3 | +------------------------+ 2 rows in set (0.00 sec)
广告