我的sql if else条件

时间:2017-03-18 04:52:03

标签: php mysql

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

需要帮助,当销售价格为0.00时,不要将百分比计算为100%

2 个答案:

答案 0 :(得分:1)

您已经混淆了ifcase 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