MySQL IF with math

Is it possible to execute mysql if / then using math?

Say ...

if product_name = "apple" then divide product_price by 4 else do no math on product_price 

A link to a tutorial on something similar, or any help / direction would be appreciated.

I ended up using the bluefeet method with that too. A model for everyone who might need in the future,

  SELECT DISTINCT 12345_parts.*, 12345_parts.install_meter + 12345_parts.meter_life - 12345_parts.prior_meter AS nextdue, CASE WHEN 12345_parts.part_name = "JET ENGINE" THEN 12345_parts.meter_life + 12345_parts.install_meter - 12345_parts.prior_meter - status_12345.meter / 4 ELSE 12345_parts.meter_life + 12345_parts.install_meter - 12345_parts.prior_meter - status_12345.meter END AS remainder FROM 12345_parts, status_12345 WHERE 12345_parts.overhaul LIKE '%HLY%' AND 12345_parts.active='ACTIVE' 
+4
source share
5 answers

You can also use the CASE statement:

 select case when product_name = 'apple' then product_price/4 else product_price end as price from yourtable 
+1
source

Try the following:

 SELECT IF(product_name = "apple", product_price / 4, product_price) price FROM products; 
+4
source

In your example, you can simply do:

 IF (product_name = "apple") BEGIN SELECT product_name, (product_price/4) FROM table END 
+1
source

IF Then Instructions

Straight from the MySQL site.

0
source

You can try this -

 select If(product_name = "apple",product_price/4,product_price) as cal from tableName 
0
source

All Articles