- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Populate null columns in a MySQL table and set values
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)
Advertisements