首先顯示值非 Null,然後在 MySQL 中顯示值 Null 的結果


我們首先建立一個表 -

mysql> create table DemoTable1357
    -> (
    -> StudentName varchar(40),
    -> StudentCountryName varchar(30)
    -> );
Query OK, 0 rows affected (0.49 sec)

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

mysql> insert into DemoTable1357 values('Chris','US');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable1357 values('David',NULL);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable1357 values('David','AUS');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable1357 values('Carol',NULL);
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable1357 values('Mike','UK');
Query OK, 1 row affected (0.15 sec)

使用 select 語句從表中顯示所有記錄 -

mysql> select * from DemoTable1357;

這將生成以下輸出 -

+-------------+--------------------+
| StudentName | StudentCountryName |
+-------------+--------------------+
| Chris       | US                 |
| David       | NULL               |
| David       | AUS                |
| Carol       | NULL               |
| Mike        | UK                 |
+-------------+--------------------+
5 rows in set (0.00 sec)

以下是顯示結果,首先顯示非 NULL 值,然後顯示 NULL 值的查詢 -

mysql> select * from DemoTable1357
    -> order by (StudentCountryName IS NULL),StudentName;

這將生成以下輸出 -

+-------------+--------------------+
| StudentName | StudentCountryName |
+-------------+--------------------+
| Chris       | US                 |
| David       | AUS                |
| Mike        | UK                 |
| Carol       | NULL               |
| David       | NULL               |
+-------------+--------------------+
5 rows in set (0.00 sec)

更新於: 05-11-2019

295 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.