在CASE语句中处理NULL

时间:2015-07-09 10:04:49

标签: php mysql

我有一个mysql查询:

SELECT CASE WHEN roomid = 1 THEN SUM(taxmoney) END END AS 'total1',
       CASE WHEN roomid = 2 THEN SUM(taxmoney) END AS 'total2', 
       DATE_FORMAT(end_at,'%Y-%m-%d') AS 'date_time' FROM match_logs GROUP BY 
       DATE_FORMAT(end_at,'%Y-%m-%d')

这是我执行时的结果:

total1 || total2 || date_time
52040  || NULL   || 2015-03-31
89803  || NULL   || 2015-04-01
45250  || 4658   || 2015-04-02

我想将NULL列中的total2更改为0。我该怎么办?

全部谢谢!

1 个答案:

答案 0 :(得分:1)

将此命令用于total2:

 CASE WHEN roomid = 2 THEN IFNULL(SUM(taxmoney),'0') END AS 'total2', 

完整的命令:

SELECT CASE WHEN roomid = 1 THEN SUM(taxmoney) END AS 'total1',
       CASE WHEN roomid = 2 THEN IFNULL(SUM(taxmoney),'0') END AS 'total2', 
       DATE_FORMAT(end_at,'%Y-%m-%d') AS 'date_time' FROM match_logs GROUP BY 
       DATE_FORMAT(end_at,'%Y-%m-%d')