Populate null columns in a MySQL table and set values

MySQLMySQLi Database

For this, you can use IS NULL property. Let us first create a table −

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

Insert some records in the table using insert command −

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)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

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

Here is the query to populate NULL columns −

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

Let us check the table records once again −

mysql> select *from DemoTable;

This will produce the following output −

+--------------+-----------------+-------------+
| ProductPrice | ProductQuantity | TotalAmount |
+--------------+-----------------+-------------+
|          100 |               2 |         200 |
|          500 |               4 |        2000 |
|         1000 |              10 |       10000 |
+--------------+-----------------+-------------+
3 rows in set (0.00 sec)
raja
Published on 25-Sep-2019 12:19:52
Advertisements