
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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)
- Related Questions & Answers
- MYSQL select DISTINCT values from two columns?
- Removing NOT NULL restriction from column in MySQL?
- Select distinct combinations from two columns in MySQL?
- Select distinct values from two columns in MySQL?
- Display only NOT NULL values from a column with NULL and NOT NULL records in MySQL
- Select distinct names from two columns in MySQL and display the result in a single column
- Concatenate two columns when one of such column values is null in MySQL
- Empty string in not-null column in MySQL?
- MySQL Select Rows where two columns do not have the same value?
- Count only null values in two different columns and display in one MySQL select statement?
- How to select ID column as null in MySQL?
- Check for NULL or NOT NULL values in a column in MySQL
- Update a column A if null, else update column B, else if both columns are not null do nothing with MySQL
- Insert default into not null column if value is null in MySQL?
- How to add a NOT NULL column in MySQL?