首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > Mysql >

mysql 多条记要判断相加减合并一条

2012-11-09 
mysql 多条记录判断相加减合并一条codepaypayflag00001100.0000000150.0010000150.00000002200.000pay 是

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
[解决办法]

SQL code
SELECT `code`,SUM(IF(payflag=0,pay,0))-SUM(IF(payflag=1,pay,0)) AS pay FROM `tablename` GROUP BY `code`
[解决办法]
SQL code
select `code`,sum((0.5-payflag)*2*pay) from tablegroup by `code` 

热点排行