如何查詢 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)
廣告