While writing a select statement in MySQL it is possible to use an IF statement, an example would be apply a tax cost to a product price or converting a 1 to Yes and 0 to No.

The syntax for the statement is

IF(expression ,true, false);

So if we wanted to convert a boolean (either on or off) from a number to Yes or No we would use

IF(fieldactive = 1,"Yes", "No");

It is possible to use the if statement in conjunction with calculations another good example would be to apply a tax charge (VAT @ 20%) if the product value needs VAT applied to it.

IF(fieldvat =1,fieldvalue * 1.2, fieldvalue) as fieldincvat

When the “fieldvat” equals 1 we then take the value of fieldvalue and multiply it by 1.2 (which is the same as adding 20% to the orginal value), else we just show the fieldvalue

Leave a Reply