僅計算兩列的空值,並顯示在一個 MySQL select 語句中?


使用 IS 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)

以下是用於僅計算兩列中的空值並顯示在一個 select 語句中的查詢 -

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)

更新於: 2020年6月30日

393 次瀏覽

啟動你的 職業

完成課程,獲得認證

開始
廣告
© . All rights reserved.