IF Function in MySQL

Profile picture for user arilio666

The IF() function returns the value if a condition is satisfied, and faithful else returns a false value if it is not satisfied.

Syntax

IF(condition, value_if_true, value_if_false)

Let us see a sample example:

select if(100<10, 'TRUE','FALSE')

mysql if function

We can see that the condition proceeded with false as 100 is not less than 10.

Example

Let us take the products table and do the IF() function on the quantity. If it is more than 90, then mark it as enough else, not enough.

if function in mysql

select product_id, name, if(quantity_in_stock > 90, 'Enough','Not Enough') from sql_inventory.products;

mysql if function

We can see that the condition works as we expected.

Conclusion:

So by this, we can get a faint idea of how the IF() function can be used in MySQL.

Tags