
- 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
Alternative to MySQL CASE WHEN in MySQL
Use IF() method as an alternative to CASE WHEN in MySQL. Let us first create a table −
mysql> create table DemoTable1593 -> ( -> PlayerScore int -> ); Query OK, 0 rows affected (0.44 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1593 values(78); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1593 values(0); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1593 values(89); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1593 values(0); Query OK, 1 row affected (0.16 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1593;
This will produce the following output −
+-------------+ | PlayerScore | +-------------+ | 78 | | 0 | | 89 | | 0 | +-------------+ 4 rows in set (0.00 sec)
Here is the query to use MySQL IF() −
mysql> select if(PlayerScore=0,'',PlayerScore) from DemoTable1593;
This will produce the following output −
+----------------------------------+ | if(PlayerScore=0,'',PlayerScore) | +----------------------------------+ | 78 | | | | 89 | | | +----------------------------------+ 4 rows in set (0.00 sec)
- Related Articles
- MySQL ORDER BY with CASE WHEN
- Perform count with CASE WHEN statement in MySQL?
- Implement MySQL CASE statement with WHEN clause
- MySQL Mass Update with CASE WHEN/ THEN/ ELSE?
- MySQL CASE WHEN with SELECT to display odd and even ids?
- Is there any alternative for CONCAT() in MySQL?
- Expression in CASE WHEN Clause doesn't work in MySQL query?
- MySQL query to find alternative records from a table
- MySQL Query to change lower case to upper case?
- MySQL case-insensitive DISTINCT?
- Get boolean result whether table exists or not using CASE WHEN in MySQL
- How to achieve case sensitive uniqueness and case insensitive search in MySQL?
- CASE WHEN column1 IS NULL THEN NULL ELSE column2 END with MySQL
- MySQL query to update every alternative row string having same values?
- Implement CASE statement with WHEN clause to check for the existence of a record in MySQL

Advertisements