
- 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
Set value only for NULL values in a MySQL table
Use IFNULL to check for NULL values and set a value using the SET command. Let us first create a table −
mysql> create table DemoTable817(Value int); Query OK, 0 rows affected (0.53 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable817 values(10); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable817 values(null); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable817 values(20); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable817 values(null); Query OK, 1 row affected (0.11 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable817;
This will produce the following output −
+-------+ | Value | +-------+ | 10 | | NULL | | 20 | | NULL | +-------+ 4 rows in set (0.00 sec)
Following is the query to set value only for NULL values −
mysql> update DemoTable817 set Value=IFNULL(Value,0); Query OK, 2 rows affected (0.17 sec) Rows matched: 4 Changed: 2 Warnings: 0
Let us check the table records once again −
mysql> select *from DemoTable817;
This will produce the following output −
+-------+ | Value | +-------+ | 10 | | 0 | | 20 | | 0 | +-------+ 4 rows in set (0.00 sec)
- Related Articles
- Set a custom value for NULL or empty values in MySQL
- Populate null columns in a MySQL table and set values
- Set 1 for NOT NULL value in MySQL
- Conditional WHERE clause in MySQL stored procedure to set a custom value for NULL values
- MySQL query to set values for NULL occurrence
- SET only two values for all the rows in a MySQL table based on conditions?
- MySQL query to replace only the NULL values from the table?
- Place a specific value for NULL values in a MySQL column
- Compare for NULL value and display value 1 for these values in a new MySQL column?
- Display only the default values set for columns in MySQL
- Set DEFAULT values for columns while creating a table in MySQL
- MySQL query to set different combinations for values in a table?
- Display only NOT NULL values from a column with NULL and NOT NULL records in 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?
- Perform mathematical calculations in a MySQL table with NULL and NON-NULL values

Advertisements