
- 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
CASE WHEN column1 IS NULL THEN NULL ELSE column2 END with MySQL
For this, you can use the CASE statement. Let us first create a table−
mysql> create table DemoTable -> ( -> Name varchar(20), -> Marks1 int, -> Marks2 int -> ); Query OK, 0 rows affected (0.72 sec)
Insert some records in the table using insert command−
mysql> insert into DemoTable values('Chris',45,null); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('David',null,78); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('Bob',67,98); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------+--------+--------+ | Name | Marks1 | Marks2 | +-------+--------+--------+ | Chris | 45 | NULL | | David | NULL | 78 | | Bob | 67 | 98 | +-------+--------+--------+ 3 rows in set (0.00 sec)
Here is the query to implement CASE WHEN −
mysql> select *, -> (case when Marks1 is null then null else Marks2 end ) as Value -> from DemoTable;
This will produce the following output −
+-------+--------+--------+-------+ | Name | Marks1 | Marks2 | Value | +-------+--------+--------+-------+ | Chris | 45 | NULL | NULL | | David | NULL | 78 | NULL | | Bob | 67 | 98 | 98 | +-------+--------+--------+-------+ 3 rows in set (0.00 sec)
- Related Articles
- MySQL Mass Update with CASE WHEN/ THEN/ ELSE?
- Sum if all rows are not null else return null in MySQL?
- Conditional NOT NULL case MySQL?
- Return null for date_format when input is null in MySQL?
- Update a column A if null, else update column B, else if both columns are not null do nothing with MySQL
- Working with NULL and IS NOT NULL in MySQL
- Display the result with not null value first and then with null value in MySQL
- When MySQL IN() function returns NULL?
- When MySQL MAKE_SET() function returns NULL?
- In which conditions, MySQL CASE statement return NULL?
- Can we use PRIMARY KEY( column1, column2) in MySQL to make pairs?
- Difference Between MySql NULL and IS NOT NULL?
- When a MySQL arithmetic expression returns NULL?
- Display only NOT NULL values from a column with NULL and NOT NULL records in MySQL
- When MySQL FIND_IN_SET() function returns NULL as output?

Advertisements