2017-03-18 97 views
1
SELECT price, sale, (IF(sale > 0) THEN ((price-sale)/price)*100 ELSE sale) AS discount FROM tbl_business_service_price 

需要解決這個問題,不想計算的百分比爲100%時的銷售價格爲0.00我的SQL,如果其他條件

回答

0

試試這個..

SELECT price, sale, IF(sale > 0, (price-sale/price) * 100, sale) AS discount FROM tbl_business_service_price 
1

你有混合使用ifcase when語法,通常看一下官方doc,就可以得到你想要的。

if溶液:

SELECT 
    price, 
    sale, 
    IF(sale > 0, ((price - sale)/price) * 100, sale) AS discount 
FROM tbl_business_service_price 

case when溶液:

SELECT 
    price, 
    sale, 
    case when sale > 0 then ((price - sale)/price) * 100 else sale end AS discount 
FROM tbl_business_service_price