
- 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
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 Articles
- Removing NOT NULL restriction from column in MySQL?
- Select distinct values from two columns in MySQL?
- Select distinct combinations from two columns in MySQL?
- MYSQL select DISTINCT values from two columns?
- 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
- Count only null values in two different columns and display in one MySQL select statement?
- How to select ID column as null in MySQL?
- Empty string in not-null column in MySQL?
- Update a column A if null, else update column B, else if both columns are not null do nothing with MySQL
- MySQL Select Rows where two columns do not have the same value?
- MySQL query to count all the column values from two columns and exclude NULL values in the total count?
- Check for NULL or NOT NULL values in a column in MySQL
- Insert default into not null column if value is null in MySQL?
