
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
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)
- Related Articles
- Set value only for NULL values in a MySQL table
- Set DEFAULT values for columns while creating a table in MySQL
- Perform mathematical calculations in a MySQL table with NULL and NON-NULL values
- How to set NULL values in a table column and insert the same
- How can I return the values of columns from MySQL table as a set of values?
- Add values of two columns considering NULL values as zero in MySQL
- Find the count of EMPTY or NULL columns in a MySQL table?
- Set a custom value for NULL or empty values in MySQL
- Set multiple values for custom columns in MySQL?
- MySQL query to set values for NULL occurrence
- Counting the number of non-null or nonzero columns in a MySQL table?
- Update all the fields in a table with null or non-null values with MySQL
- Return only the non-empty and non-null values from a table and fill the empty and NULL values with the corresponding column values in MySQL?
- Count only null values in two different columns and display in one MySQL select statement?
- How to find records with a null value in a set of columns with MySQL

Advertisements