使用 MySQL 更新表中的所有欄位為 null 或非 null 值


讓我們先建立一個表 −

mysql> create table DemoTable
   -> (
   -> Id int,
   -> Name varchar(20)
   -> );
Query OK, 0 rows affected (0.58 sec)

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

mysql> insert into DemoTable values(10,NULL);
Query OK, 1 row affected (0.32 sec)
mysql> insert into DemoTable values(NULL,'David');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable values(NULL,NULL);
Query OK, 1 row affected (0.15 sec)

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

mysql> select * from DemoTable;

這將產生以下輸出 −

+------+-------+
|   Id | Name  |
+------+-------+
|   10 | NULL  |
| NULL | David |
| NULL | NULL  |
+------+-------+
3 rows in set (0.00 sec)

以下是更新表中所有欄位(null 或非 null 值)的查詢 −

mysql> update DemoTable set Name='Robert' where Id IS NULL or Name IS NULL;
Query OK, 3 rows affected (0.22 sec)
Rows matched: 3 Changed: 3 Warnings: 0

讓我們再次檢查表記錄 −

mysql> select * from DemoTable;

這將產生以下輸出 −

+------+--------+
|   Id | Name   |
+------+--------+
|   10 | Robert |
| NULL | Robert |
| NULL | Robert |
+------+--------+
3 rows in set (0.00 sec)

更新於: 26-Feb-2020

660 瀏覽

開啟您的 職業

完成課程獲取認證

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