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(pay*(case payflag when 1 then -1 else 1 end)) as pay
from [table]
group by code
第二种:
select code,sum(if(payflag=0,pay,-1*pay))
from tt
group by code
第三种
select code, sum(pay*(cos(PI()*payflag))) as pay
from [tablename]
group by code