2013-01-04 82 views
4

是否有可能做一個MySQL如果/然後語句與數學?MySQL如果然後用數學

說...

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

鏈接到一個教程這樣的事情或者任何幫助/方向,將不勝感激。

我用bluefeet的方法結束了這個。樣品爲別人,可能需要在未來,

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' 

回答

1

你也可以使用一個CASE聲明:

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

試試這個:

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

你可以試試這個 -

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

從你的榜樣,你可以這樣做:

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

這個解決方案最適合我的需求。謝謝。 – user1949190