
- 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
Display 1 for NULL values in MySQL
Let us first create a table −
mysql> create table DemoTable1963 ( Counter int ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1963 values(20); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1963 values(NULL); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1963 values(99); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1963 values(49); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1963 values(NULL); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1963;
This will produce the following output −
+---------+ | Counter | +---------+ | 20 | | NULL | | 99 | | 49 | | NULL | +---------+ 5 rows in set (0.00 sec)
Here is the query to display 1 for NULL values in MySQL:
mysql> update DemoTable1963 set Counter=IFNULL(Counter,0)+1 where Counter IS NULL; Query OK, 2 rows affected (0.00 sec) Rows matched: 2 Changed: 2 Warnings: 0
Let us check the table records once again −
mysql> select * from DemoTable1963;
This will produce the following output −
+---------+ | Counter | +---------+ | 20 | | 1 | | 99 | | 49 | | 1 | +---------+ 5 rows in set (0.00 sec)
- Related Articles
- Compare for NULL value and display value 1 for these values in a new MySQL column?
- Ignore null values in MySQL and display rest of the values
- Display and concatenate records ignoring NULL values in MySQL
- Display first non-null values with coalesce() in MySQL?
- Display only NOT NULL values from a column with NULL and NOT NULL records in MySQL
- Check for NULL or NOT NULL values in a column in MySQL
- Set 1 for NOT NULL value in MySQL
- MySQL query to display only the empty and NULL values together?
- MySQL query to set values for NULL occurrence
- MySQL update column to NULL for blank values
- Display records ignoring NULL in MySQL
- Set value only for NULL values in a MySQL table
- MySQL query to compare and display only the rows with NULL values
- Ignore NULL values from separate tables in a single MySQL query and display count of NOT NULL records
- The best data type to store 0, 1, null values in MySQL?

Advertisements