Mysql 存在空值时,从两个表中减去两个值 选择p.partnerid, 金额(如果c.amount不为空,则c.amount为0结束)作为金额, 求和(c.netamt不为空时,则为c.netamt,否则为0结束)作为总计, 总和(当(c.netamt-d.paidamount)不为空时,则(c.netamt-d.paidamount)否则为0结束)作为剩余的装入, 求和(当d.paidamount不为空时,则d.paidamount否则为0结束)为paidamt 来自customerinfo c 在c.accno=d.accno上左连接每日付款d 在c.partnerid=p.partnerid上右键加入partnerinfo p 其中(d.paiddate为空或(d.paiddate>='2011-3-15'和d.paiddate

Mysql 存在空值时,从两个表中减去两个值 选择p.partnerid, 金额(如果c.amount不为空,则c.amount为0结束)作为金额, 求和(c.netamt不为空时,则为c.netamt,否则为0结束)作为总计, 总和(当(c.netamt-d.paidamount)不为空时,则(c.netamt-d.paidamount)否则为0结束)作为剩余的装入, 求和(当d.paidamount不为空时,则d.paidamount否则为0结束)为paidamt 来自customerinfo c 在c.accno=d.accno上左连接每日付款d 在c.partnerid=p.partnerid上右键加入partnerinfo p 其中(d.paiddate为空或(d.paiddate>='2011-3-15'和d.paiddate,mysql,Mysql,使用该函数删除案例,并简化计算: select p.partnerid, sum(case when c.amount is not null then c.amount else 0 end) as amount, sum(case when c.netamt is not null then c.netamt else 0 end) as total, sum(case when (c.netamt - d.paidamount) is not null then (c.netamt - d

使用该函数删除
案例
,并简化计算:

select p.partnerid,
sum(case when c.amount is not null then c.amount else 0 end) as amount,
sum(case when c.netamt is not null then c.netamt else 0 end) as total,
sum(case when (c.netamt - d.paidamount) is not null then (c.netamt - d.paidamount) else 0 end) as remainingamount,
sum(case when d.paidamount is not null then d.paidamount else 0 end) as paidamt
from customerinfo c
left join dailypayments d on c.accno = d.accno
right join partnerinfo p on c.partnerid = p.partnerid
where (d.paiddate is null or (d.paiddate >= '2011-3-15' and d.paiddate <= '2012-6-13')) and  p.manager = 7 group by p.partnerid
sum(c.netamt - ifnull(d.paidamount, 0)) as remainingamount,