填充 MySQL 表中的空列並設定值


為此,您可以使用 IS NULL 屬性。讓我們首先建立一個表 −

mysql> create table DemoTable
(
   ProductPrice int,
   ProductQuantity int,
   TotalAmount int
);
Query OK, 0 rows affected (1.22 sec)

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

mysql> insert into DemoTable(ProductPrice,ProductQuantity) values(100,2);
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable(ProductPrice,ProductQuantity) values(500,4);
Query OK, 1 row affected (0.23 sec)
mysql> insert into DemoTable(ProductPrice,ProductQuantity) values(1000,10);
Query OK, 1 row affected (0.21 sec)

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

mysql> select *from DemoTable;

這將產生以下輸出 −

+--------------+-----------------+-------------+
| ProductPrice | ProductQuantity | TotalAmount |
+--------------+-----------------+-------------+
|          100 |               2 |        NULL |
|          500 |               4 |        NULL |
|         1000 |              10 |        NULL |
+--------------+-----------------+-------------+
3 rows in set (0.00 sec)

以下是填充 NULL 列的查詢 −

mysql> update DemoTable set TotalAmount=(ProductPrice*ProductQuantity) where
TotalAmount IS NULL;
Query OK, 3 rows affected (0.20 sec)
Rows matched: 3 Changed: 3 Warnings: 0

讓我們再次檢查表記錄 −

mysql> select *from DemoTable;

這將產生以下輸出 −

+--------------+-----------------+-------------+
| ProductPrice | ProductQuantity | TotalAmount |
+--------------+-----------------+-------------+
|          100 |               2 |         200 |
|          500 |               4 |        2000 |
|         1000 |              10 |       10000 |
+--------------+-----------------+-------------+
3 rows in set (0.00 sec)

更新日期: 2019 年 9 月 25 日

594 次瀏覽

開啟你的 職業

透過完成課程獲得認證

開始吧
廣告
© . All rights reserved.