只统计两列中的null值并在一个MySQL选择语句中显示?
使用IS NULL来测试NULL值。让我们首先创建一个表−
mysql> create table DemoTable
-> (
-> Number1 int,
-> Number2 int
-> );
Query OK, 0 rows affected (0.62 sec)
使用插入命令向表中插入一些记录−
mysql> insert into DemoTable values(1,NULL);
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable values(NULL,NULL);
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values(3,NULL);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values(NULL,90);
Query OK, 1 row affected (0.11 sec)
使用选择语句显示表中的所有记录−
mysql> select *from DemoTable;
阅读更多:MySQL 教程
输出
这将产生以下输出−
+---------+---------+
| Number1 | Number2 |
+---------+---------+
| 1 | NULL |
| NULL | NULL |
| 3 | NULL |
| NULL | 90 |
+---------+---------+
4 rows in set (0.00 sec)
以下是统计两个不同列中的null值并在一个选择语句中显示的查询−
mysql> select
-> (select count(*) from DemoTable where Number1 is null) as FirstColumnNullValue,
-> (select count(*) from DemoTable where Number2 is null) as SecondColumnNullValue
-> ;
输出
这将产生以下输出−
+----------------------+-----------------------+
| FirstColumnNullValue | SecondColumnNullValue |
+----------------------+-----------------------+
| 2 | 3 |
+----------------------+-----------------------+
1 row in set (0.00 sec)