表1中列值A求和 - 表2中列值B求和

求和:

select sum(A) from table1 where id = '1';

select sum(B) from table2 where id = '1';

 

若求和值为Null,需要转换,否组会报错

通过IFNULL转换,转换格式:IFNULL(字段,0)

调整后如下:

select IFNULL(sum(A),0)  from table1 where id = '1';

select IFNULL(sum(B),0)  from table2 where id = '1';

 

相减:

select (select IFNULL(sum(A),0)  from table1 where id = '1') - (select IFNULL(sum(B),0)  from table2 where id = '1')

 

posted @ 2020-12-30 17:46  linma  阅读(160)  评论(0)    收藏  举报