MySQL Wildcards

MySQL Wildcards

MySQL Aliases

MySQL Aliases

MySQL Data Types

MySQL Data Types

MySQL Interview Questions

MySQL Interview Questions and Answers


MySQL Case Operator

The MySQL allows you to write complex conditional construct with the help of CASE operator. In other words, CASE operator for stored programs implements a complex conditional construct.

MySQL CASE OPERATOR Syntax

To write complex conditional construct, use the following syntax:

Syntax

SELECT Col1, Col2 .....,
CASE WHEN [condition] THEN result 
[ELSE result] 
END
FROM tablename;

Parameters:

tablename: The table name from which you want to fetch the records.

condition: To check conditions is true or not.

result: If condition fails, return default result.

MySQL CASE OPERATOR example

The following MySQL operator is used to write complex conditional construct in a stored program:

Example

SELECT firstName, lastName, address,
CASE WHEN firstName = 'Dilip' THEN 'Found' 
ELSE 'Not Found' END AS Status
FROM tblCustomerLoan;

Note:

In the above example, we are checking each string in "firstName" column, if value = "Dilip" then return "Found" value else return "Not Found" value as result.

You can use MySQL Command Line Client to write complex conditional construct in a stored program. It will look like this: