填充 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)
广告