如果不是空的Mysql SUM唯一

时间:2014-12-11 18:14:27

标签: mysql

示例:

id | c1 | c2
1  | aa | 5
2  |    | 1
3  |    | 3
4  | aa | 5

我想选择SUM(c2),如果c1上有重复的值,那么它只会计算一行(重复c1总是让它们相同c2),但是我还希望如果c1为空 - 那么无论如何都要计算他的c2

示例的输出应为:9(5 + 1 + 3)

1 个答案:

答案 0 :(得分:0)

我认为这应该有效(但查询时间有点贵)

select sum(sum(c2),(select sum(c2) from table where c1 is not null)) from table where c1 is null group by c1;