
- 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
How does MYSQL control flow function CASE works?
MySQL CASE statement is a flow control function that allows us to build conditions inside a query such as SELECT or WHERE clause. We have two syntaxes of CASE statement
Syntax-1
CASE val WHEN compare_val1 THEN result1 WHEN compare_val2 THEN result2 . . . Else result END
Here in this 1st syntax, if the val is equal to compare_val1 then the CASE statement returns result1. If the val is equal to compare_val2 then the CASE statement returns result2 and so on.
In case if the val does not match any compare_val then the CASE statement returns the result specified in ELSE clause.
Example
mysql> Select CASE 100 -> WHEN 100 THEN 'It is matched' -> WHEN 200 THEN 'It is not matched' -> ELSE 'No use of this Number' -> END as 'Matching the Number'; +---------------------+ | Matching the Number | +---------------------+ | It is matched | +---------------------+ 1 row in set (0.06 sec)
Syntax-2
CASE WHEN condition_1 THEN result1 WHEN condition_2 THEN result2 . . . Else result END
Here in this 2nd syntax, the CASE statement returns result1, result2, etc. if the condition is true. In case if all the conditions are false the CASE statement returns the result specified in ELSE clause.
Example
mysql> Select CASE -> WHEN (100 = 100) THEN 'It is Matched' -> WHEN (200 = 100) Then 'It is Not Matched' -> Else 'No use of Number' -> END as 'Matching the Number'; +---------------------+ | Matching the Number | +---------------------+ | It is Matched | +---------------------+ 1 row in set (0.00 sec)
- Related Articles
- How MySQL NULLIF() control flow function is similar to CASE statement?
- What is the use of MySQL IFNULL() control flow function?
- What is the use of MySQL NULLIF() control flow function?
- How MySQL DATEDIFF() function works?
- How EXPORT_SET() function works in MySQL?
- How does MySQL CASE work?
- In MySQL, how IN() comparison function works?
- Difference between flow control and congestion control
- How does digital thermometer works?
- Static Control Flow in Java
- Control flow alterations in Ruby
- How does MySQL IF() function work?
- How does jQuery event namespace works?
- How does JavaScript focus() method works?
- How does Secure Hash Algorithm works?

Advertisements