GROUP结果值,如果为null

时间:2013-10-19 07:45:29

标签: mysql group-by sum ifnull

我无法执行此查询:

TABLE:

------------
NOTES  | TOTAL
------------
NULL    |  23
NULL    |  12
pay1    | -13
pay2    | -23
-------------

结果:

------------
NOTES   | TOTAL
------------
SELL    |  35
pay1    | -13
pay2    | -23
-------------

我需要对所有NULL字段和名称为“SELL”的组进行求和。

3 个答案:

答案 0 :(得分:1)

select IFNULL(NOTES,'SELL') n,SUM(TOTAL) FROM records GROUP BY n;

答案 1 :(得分:0)

使用IF条件语句检查名称列为NULL值,并替换为" SELL" 文本。

select IF(name is  NULL, "SELL", name) as name, sum(total) as total from content group by name;

答案 2 :(得分:0)

SELECT 
IFNULL(NOTES,'SELL')as NOTES,
SUM(TOTAL)  as TOTAL
FROM records 
GROUP BY notes
ORDER BY IFNULL(NOTES,'a')