您好,欢迎来到爱尚美食网。
搜索
您的当前位置:首页sql两个字段相减语句

sql两个字段相减语句

来源:爱尚美食网


方法二

create table 表1( 字段1 varchar(50), 字段2 int)
insert into 表1
select '111001' , 10 union all
select '111002' , 9 union all
select '111003' , 12 union all
select '111004' , 23

create table 表2( 字段1 varchar(50), 字段2 int)
insert into 表2
select '111001' , 3 union all
select '111002' , 2 union all
select '111003' , 12

select a.字段1, (a.[字段2] - (isnull(b.[字段2],0)) ) as 字段2 from 表1 a left join 表2 b
on a.[字段1] = b.[字段1]

结果: 字段1 字段2
111001 7
111002 7
111003 0(不显示)
111004 23


方法三


有两个相同的表t1和t2,都有字段a(字符),b(数值),c(数值),内容如下:
t1 t2
a b c a b c
a1 10 20 a2 2 10
a2 10 20 a3 3 15
a3 10 20
a4 10 20

select t1.a as a,(t1.b - isnull(t2.b,0)) as b,(t1.c - isnull(t2.c,0)) as c from [t1] left join [t2] on (t1.a = t2.a)

Copyright © 2019- aivalley.cn 版权所有

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务