
- 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
Return value from a row if it is NOT NULL, else return the other row value in another column with MySQL
For this, you can use IFNULL(). Let us first create a table −
mysql> create table DemoTable ( FirstName varchar(100), LastName varchar(100) ); Query OK, 0 rows affected (0.62 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('John','Doe'); Query OK, 1 row affected (0.29 sec) mysql> insert into DemoTable values(NULL,'Taylor'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('David',NULL); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(NULL,'Miller'); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-----------+----------+ | FirstName | LastName | +-----------+----------+ | John | Doe | | NULL | Taylor | | David | NULL | | NULL | Miller | +-----------+----------+ 4 rows in set (0.00 sec)
Following is the query to return value from a row if it is NOT NULL, else return the other row value in another column −
mysql> select ifnull(FirstName,LastName) as Result from DemoTable;
This will produce the following output −
+--------+ | Result | +--------+ | John | | Taylor | | David | | Miller | +--------+ 4 rows in set (0.00 sec)
- Related Articles
- Write a single MySQL query to return the ID from the corresponding row which is a NOT NULL value
- Sum if all rows are not null else return null in MySQL?
- Multiplying column with NULL row in MySQL?
- Update a column A if null, else update column B, else if both columns are not null do nothing with MySQL
- Return only a single row from duplicate rows with MySQL
- Select minimum row value from a column with corresponding duplicate column values in MySQL
- Insert default into not null column if value is null in MySQL?
- How to check if any value is Null in a MySQL table single row?
- Python Pandas – Find the maximum value of a column and return its corresponding row values
- How to do a sum of previous row value with the current row and display the result in another row with MySQL cross join?
- MySQL insert a value to specific row and column
- MySQL stored procedure to return a column value?
- Count the same value of each row in a MySQL column?
- Return maximum value from records in MySQL
- Adding a column whose value is not null by default in MySQL?

Advertisements