仅计算两个不同列中的空值并在一个 MySQL 选择语句中显示?

mysqlmysqli database更新于 2025/5/26 10:20:17

使用 IS NULL 测试 NULL 值。让我们首先创建一个表 −

mysql> create table DemoTable
   -> (
   -> Number1 int,
   -> Number2 int
   -> );
Query OK, 0 rows affected (0.62 sec)

使用 insert 命令在表中插入一些记录 −

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)

使用 select 语句显示表中的所有记录 −

mysql> select *from DemoTable;

输出

这将产生以下输出 −

+---------+---------+
| Number1 | Number2 |
+---------+---------+
|      1 | NULL     |
|   NULL | NULL     |
|      3 | NULL     |
|   NULL | 90       |
+---------+---------+
4 rows in set (0.00 sec)

以下查询仅计算两个不同列中的空值并在一个选择语句中显示 −

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)

相关文章


有用资源