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,如果其他條件
SELECT price, sale, (IF(sale > 0) THEN ((price-sale)/price)*100 ELSE sale) AS discount FROM tbl_business_service_price
需要解決這個問題,不想計算的百分比爲100%時的銷售價格爲0.00我的SQL,如果其他條件
試試這個..
SELECT price, sale, IF(sale > 0, (price-sale/price) * 100, sale) AS discount FROM tbl_business_service_price
你有混合使用if
和case 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