ab-logo

Code. Models. Analysis. Decisions.


Using Functions and Statements for Decisions in MySQL

If Function and CASE WHEN Statement

The ability to make decisions with MySQL is the basis for many more advanced data manipulations. Decisions can be made using the IF function, which operates very much like the Excel IF function with a few exceptions. If takes three exressions. If the first expression is True, Not NULL or Zero, then it returns the second expression, otherwise it returns the third expression. If functions can be nested within eachother, but it is often preferable to write more clear syntax and this is where CASE...WHEN statements come in.

The CASE statement is also conditional. This structure can be handy when you are trying to classify data into more than two categories.

You may also be interested in:

Creating Databases & Loading Data

Basic MySQL Queries

INNER and OUTER JOINS

Using MySQL Text Functions

Statistics with MySQL

Download the data and code.