mysql 多条记录判断相加减合并一条
code pay payflag
00001 100.00 0
00001 50.00 1
00001 50.00 0
00002 200.00 0
pay 是 decimal,payflag 0 是付款, 1 是退款
把code相同并payflag=0的pay相加减去payflag=1的pay得到下面这下的数据
code pay
00001 100.00
00002 200.00
第一次发帖,急着要。。在线等。。。
[解决办法]
select code,sum(if(payflag=0,pay,-1*pay))
from tt group by code
[解决办法]
SELECT `code`,SUM(IF(payflag=0,pay,0))-SUM(IF(payflag=1,pay,0)) AS pay FROM `tablename` GROUP BY `code`
[解决办法]
select `code`,sum((0.5-payflag)*2*pay) from tablegroup by `code`