避免MySql中的NULL引用结果

时间:2017-11-10 10:40:12

标签: mysql

我正在运行此查询。工作正常但是如果找到NULL结果,那么它会在我的应用程序中创建异常错误。如何避免NULL参考

SELECT SUM(pointsEarned) as earned, SUM(pointsRedeemed) as redeemed 
FROM `salesData` 
WHERE dealerID = @dealerID and status = 'active'

1 个答案:

答案 0 :(得分:1)

COALESCE周围使用SUM

SELECT COALESCE(SUM(pointsEarned),0) earned, COALESCE(SUM(pointsRedeemed),0) redeemed 
FROM salesData 
WHERE dealerID = @dealerID
AND status = 'active'