
- 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
Compare for NULL value and display value 1 for these values in a new MySQL column?
For this, use IF() along with IS NULL property. Let us first create a table −
mysql> create table DemoTable ( Name varchar(100), CountryName varchar(100) ); Query OK, 0 rows affected (0.70 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('Chris','US'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Mike',NULL); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(Name) values('David'); Query OK, 1 row affected (0.38 sec) mysql> insert into DemoTable values('Bob','AUS'); Query OK, 1 row affected (0.45 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------+-------------+ | Name | CountryName | +-------+-------------+ | Chris | US | | Mike | NULL | | David | NULL | | Bob | AUS | +-------+-------------+ 4 rows in set (0.00 sec)
Following is the query to compare for NULL value and display value 1 for these values in a new MySQL column −
mysql> select Name,CountryName,If (CountryName IS NULL,1,0) AS CountDemo from DemoTable;
This will produce the following output −
+-------+-------------+-----------+ | Name | CountryName | CountDemo | +-------+-------------+-----------+ | Chris | US | 0 | | Mike | NULL | 1 | | David | NULL | 1 | | Bob | AUS | 0 | +-------+-------------+-----------+ 4 rows in set (0.00 sec)
- Related Articles
- Place a specific value for NULL values in a MySQL column
- Display 1 for NULL values in MySQL
- Set 1 for NOT NULL value in MySQL
- Implement two conditions for a single column in MySQL for null and empty value
- Set value only for NULL values in a MySQL table
- Fix a specific column value and display random values for rest of the rows in MySQL
- Set a custom value for NULL or empty values in MySQL
- Check for NULL or NOT NULL values in a column in MySQL
- Set the NULL values to 0 and display the entire column in a new column with MySQL SELECT
- Display custom text in a new column on the basis of null values in MySQL?
- MySQL update column to NULL for blank values
- Set a specific value for the first three column values in MySQL?
- Display only NOT NULL values from a column with NULL and NOT NULL records in MySQL
- Display the result with not null value first and then with null value in MySQL
- Display NULL and NOT NULL records except a single specific value in MySQL

Advertisements