SELECT price, sale, (IF(sale > 0) THEN ((price-sale)/price)*100 ELSE sale) AS discount FROM tbl_business_service_price
需要帮助,当销售价格为0.00时,不要将百分比计算为100%
答案 0 :(得分:1)
您已经混淆了if
和case when
语法,通常会看一下官方文档,您可以得到您想要的内容。
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
答案 1 :(得分:0)
尝试一下..
SELECT price, sale, IF(sale > 0, (price-sale/price) * 100, sale) AS discount FROM tbl_business_service_price