忽略 MySQL 中的空值並顯示其他值


使用 IS NOT NULL 查詢非空值並顯示出來。我們先建立一個表 −

mysql> create table DemoTable1458
   -> (
   -> StudentName varchar(20),
   -> StudentScore int
   -> );
Query OK, 0 rows affected (0.52 sec)

使用 insert 命令在表中插入一些記錄 −

mysql> insert into DemoTable1458 values('Chris Brown',56);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable1458 values('David Miller',NULL);
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable1458 values('John Doe',78);
Query OK, 1 row affected (0.08 sec)
mysql> insert into DemoTable1458 values('Adam Smith',NULL);
Query OK, 1 row affected (0.12 sec)

使用 select 語句顯示錶中的所有記錄 −

mysql> select * from DemoTable1458;

這將產生以下輸出 −

+--------------+--------------+
| StudentName  | StudentScore |
+--------------+--------------+
| Chris Brown  |           56 |
| David Miller |         NULL |
| John Doe     |           78 |
| Adam Smith   |         NULL |
+--------------+--------------+
4 rows in set (0.00 sec)

以下是對忽略空值的查詢 −

mysql> select StudentName,StudentScore from DemoTable1458
   -> where StudentScore IS NOT NULL;

這將產生以下輸出 −

+-------------+--------------+
| StudentName | StudentScore |
+-------------+--------------+
| Chris Brown |           56 |
| John Doe    |           78 |
+-------------+--------------+
2 rows in set (0.00 sec)

更新於: 2019 年 12 月 10 日

1 千次以上瀏覽

啟動你的職業生涯

完成課程獲取認證

開始學習
廣告
© . All rights reserved.