我有一个很长的 sql 查询,用于计算有关付款和发货的一些事情。在某些情况下,该值为 null。
我认为这是因为除以 0。
这是我查询的一小部分:
ROUND(sum(case when shipping_method = 'c' AND (paid_amount - shipping_costs) < 70 then 1 end) * 100 / sum(case when shipping_method = 'c' then 1 end),2) as co_less_70,
我认为当这部分为 0 时:sum(case when shipping_method = 'colissimo' then 1 end),2)
我的查询显示为空。有没有办法为此 co_less_70
列分配默认值?
最佳答案
是的。您可以使用 COALESCE()
函数:
http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_coalesce
Returns the first non-NULL value in the list, or NULL if there are no non-NULL values.
在您的代码中:
COALESCE(
ROUND(sum(case when shipping_method = 'c' AND (paid_amount - shipping_costs) < 70 then 1 end) * 100 / sum(case when shipping_method = 'c' then 1 end),2),
0
) AS co_less_70
关于如果所选值为空,mysql 显示 0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8574772/