查找MySQL表中的EMPTY或NULL列的计数?
让我们首先创建一个表-
create table DemoTable781 ( Name varchar(100) );
使用插入命令在表中插入一些记录-
insert into DemoTable781 values(''); insert into DemoTable781 values('Chris'); insert into DemoTable781 values(''); insert into DemoTable781 values(null); insert into DemoTable781 values(null); insert into DemoTable781 values(''); insert into DemoTable781 values(null);
使用select语句显示表中的所有记录-
select *from DemoTable781;
这将产生以下输出-
+-------+ | Name | +-------+ | | | Chris | | | | NULL | | NULL | | | | NULL | +-------+ 7 rows in set (0.00 sec)
这是查询以查找MySQL表中EMPTY或NULL列的数量-
(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)