SELECT not null column from two columns in MySQL?


There are lots of ways to select NOT NULL column from two columns. The syntaxes are as follows:

Case 1: Use IFNULL() function.

The syntax is as follows:

SELECT IFNULL(yourColumnName1,yourColumnName2) as anyVariableName from yourTableName;

Case 2: Use coalesce() function.

The syntax is as follows:

SELECT COALESCE(yourColumnName1,yourColumnName2) as anyVariableName from yourTableName;

Case 3: Use CASE statement.

The syntax is as follows:

SELECT CASE
WHEN yourColumnName1 IS NOT NULL THEN yourColumnName1 ELSE yourColumnName2
END AS anyVariableName
FROM yourTableName;

Case 4: Use only IF().

The syntax is as follows:

SELECT IF (yourColumnName1 ISNULL,yourColumnName2,yourColumnName1) AS NotNULLValue FROM SelectNotNullColumnsDemo;

To understand the above syntax, let us create a table. The query to create a table is as follows:

mysql> create table SelectNotNullColumnsDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> Name varchar(20),
   -> Age int
   -> ,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.86 sec)

Insert some records in the table using insert command. The query is as follows:

mysql> insert into SelectNotNullColumnsDemo(Name,Age) values('John',NULL);
Query OK, 1 row affected (0.16 sec)
mysql> insert into SelectNotNullColumnsDemo(Name,Age) values(NULL,23);
Query OK, 1 row affected (0.16 sec)

Display all records from the table using select statement. The query is as follows:

mysql> select *from SelectNotNullColumnsDemo;

The following is the output:

+----+------+------+
| Id | Name | Age  |
+----+------+------+
|  1 | John | NULL |
|  2 | NULL |   23 |
+----+------+------+
2 rows in set (0.00 sec)

Here is the query to select not null values from two columns.

Case 1: IFNULL()

The query is as follows:

mysql> select ifnull(Name,Age) as NotNULLValue from SelectNotNullColumnsDemo;

The following is the output:

+--------------+
| NotNULLValue |
+--------------+
| John         |
| 23           |
+--------------+
2 rows in set (0.00 sec)

Case 2: Coalesce

The query is as follows:

mysql> select coalesce(Name,Age) as NotNULLValue from SelectNotNullColumnsDemo;

The following is the output:

+--------------+
| NotNULLValue |
+--------------+
| John         |
| 23           |
+--------------+
2 rows in set (0.00 sec)

Case 3: CASE

The query is as follows:

mysql> select case
   -> when Name is not null then Name else Age
   -> end as NotNULLValue
   -> from SelectNotNullColumnsDemo;

The following is the output:

+--------------+
| NotNULLValue |
+--------------+
| John         |
| 23           |
+--------------+
2 rows in set (0.00 sec)

Case 4: IF()

The query is as follows:

mysql> select if(Name is NULL,Age,Name) as NotNULLValue from SelectNotNullColumnsDemo;

The following is the output:

+--------------+
| NotNULLValue |
+--------------+
| John         |
| 23           |
+--------------+
2 rows in set (0.00 sec)

Updated on: 30-Jul-2019

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements