Monday, July 31, 2023

MySQL CASE Statement

       MySQL CASE Statement

The CASE statement goes through conditions and returns a value when the first condition is met (like an if-then-else statement). So, once a condition is true, it will stop reading and return the result. If no conditions are true, it returns the value in the ELSE clause.

If there is no ELSE part and no conditions are true, it returns NULL.

CASE Syntax

Demo Database

Below is a selection from the “OrderDetails” table in the sample database:

OrderDetails

MySQL CASE Examples

The following SQL goes through conditions and returns a value when the first condition is met:

Example

The following SQL will order the customers by City. However, if City is NULL, then order by Country:

Example

No comments:

Post a Comment

Building Static Website(part6) HTML Lists

  Building Static Website (part6) HTML Lists Today, let us add some lists to our detailed view section by using html lists. Lists: List is a...