
- 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
Why should we use MySQL CASE Statement?
Use MySQL CASE for a fixed number of arguments.
The syntax is as follows
SELECT *, CASE WHEN yourColumName1>yourColumName2 THEN 'yourMessage1' ELSE 'yourMessage2' END AS anyAliasName FROM yourTableName;
To understand the above syntax, let us create a table. The query to create a table is as follows
mysql> create table CaseFunctionDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Value1 int, -> Value2 int -> ); Query OK, 0 rows affected (0.56 sec)
Insert some records in the table using insert command.
The query is as follows
mysql> insert into CaseFunctionDemo(Value1,Value2) values(10,20); Query OK, 1 row affected (0.21 sec) mysql> insert into CaseFunctionDemo(Value1,Value2) values(100,40); Query OK, 1 row affected (0.10 sec) mysql> insert into CaseFunctionDemo(Value1,Value2) values(0,20); Query OK, 1 row affected (0.15 sec) mysql> insert into CaseFunctionDemo(Value1,Value2) values(0,-50); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement.
The query is as follows
mysql> select *from CaseFunctionDemo;
The following is the output
+----+--------+--------+ | Id | Value1 | Value2 | +----+--------+--------+ | 1 | 10 | 20 | | 2 | 100 | 40 | | 3 | 0 | 20 | | 4 | 0 | -50 | +----+--------+--------+ 4 rows in set (0.00 sec)
Here is the query for CASE statement
mysql> select*, case when Value1>Value2 then 'Value1 is Greater' else 'Value2 is Greater' end AS Comparision from CaseFunctionDemo;
The following is the output
+----+--------+--------+-------------------+ | Id | Value1 | Value2 | Comparision | +----+--------+--------+-------------------+ | 1 | 10 | 20 | Value2 is Greater | | 2 | 100 | 40 | Value1 is Greater | | 3 | 0 | 20 | Value2 is Greater | | 4 | 0 | -50 | Value1 is Greater | +----+--------+--------+-------------------+ 4 rows in set (0.00 sec)
- Related Articles
- Can we use WHERE clause inside MySQL CASE statement?
- Why should we use element in JavaScript?
- Why we should use set.seed in R?
- Why do we use import statement in Java? Where it should be included in the class?
- Why should we not use ++, -- operators in JavaScript?
- How to use MySQL CASE statement while using UPDATE Query?
- MySQL case statement inside a select statement?
- What is SciPy and why should we use it?
- Why should we not use an umbrella during lightning?
- Why we should not use tables for HTML Layout?
- How can we use SIGNAL statement with MySQL triggers?
- Why we should use whole string in Java regular expression
- Why should we use fossil fuels only when absolutely necessary?
- How can we use a MySQL subquery with INSERT statement?
- Can we use SELECT NULL statement in a MySQL query?

Advertisements