+-
mysql – 在SELECT语句中SQL除以零错误
我是SQL的新手,可以用手.我在SELECT语句中得到以下错误:

SUM(Cast((replace(replace(replace (p.[Total Sales], '$', ''), '(','-'),  ')','')) as money)) - SUM(Cast((replace(replace(replace (p.[Total Cost], '$', ''), '(','-'),  ')','')) as money)) / SUM(Cast((replace(replace(replace (p.[Total Sales], '$', ''), '(','-'),  ')','')) as money)) as new_bal

我知道我需要使用CASE,但我不确定应用程序.

最佳答案
你可以在下面使用 NULLIF:

NULLIF(表达式1,0)

在你的情况下它将是:

SUM(Cast((replace(replace(replace (p.[Total Sales], '$', ''), '(','-'),  ')','')) as money)) - SUM(Cast((replace(replace(replace (p.[Total Cost], '$', ''), '(','-'),  ')','')) as money)) / NULLIF(SUM(Cast((replace(replace(replace (p.[Total Sales], '$', ''), '(','-'),  ')','')) as money)),0) as new_bal
点击查看更多相关文章

转载注明原文:mysql – 在SELECT语句中SQL除以零错误 - 乐贴网