IF Condition in MySql – Syntax, Tutorial

How to use if() function in MySql . You have probably used if condition in programming, have you used if condition in mysql.  In this tutorial i’ll explain how to use if condition in MySql.

IF Condition in MySql

Syntax –

To understand the if condition in MySql let’s check the example.

It’s a simple if condition which means if the value of timestamp column is NULL then prints 1 otherwise 0.

Some more examples.

How to compare NULL values in MySql .

NOTE – There is another IF statement in MySql version 5.6 which is different from if() function.

MySql Query using if() Function

To understand it’s proper usage. Let’s create a table emp_data.

Print the name and designation of all the employee either Manager or in other category.

If condition with aggregate functions

Let’s take another table products.

We want to know how many products are in active, disabled and pending state. So let’s write a query.

You can achieve the same result through group by without using IF condition.

Difference between Inner Join and Left Join .

Subscribe Our Tutorials

Get Latest Updates on Facebook

MySql Interview Questions.

For more reference :

MySql Documentation

MySql Recommended Books

MySql Books on Amazon.com

MySql Books on Amazon.in

MySql Books on Flipkart


About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.
Tagged , . Bookmark the permalink.