我在用
select sum(value1-value2) as Result from tbl
它工作正常,但是当我尝试使用时
select id,name,sum(value1-value2) as Result from tbl
这没用。
有什么解决办法?
group by
如果您想要每个 id/name 的值,则需要 a :
select id, name, sum(value1-value2) as Result
from tbl
group by id, name;
否则,您需要将它们放在聚合函数中:
select min(id), min(name), sum(value1-value2) as Result
from tbl;
或者,如果您只想要每行的差异,则省略sum()
:
select id, name, (value1-value2) as Result
from tbl;
当你使用 sum() 时,你应该添加 group by
select id,name,sum(value1-value2) as Result
from tbl
group by id, name