sql 两个字段相减语句本文章搜索了大量来自网络的关于sql 两个字段相减语句与函数代码,举例说明了两个字段相减做法。

sql 两个字段相减语句

本文章搜索了大量来自网络的关于sql 两个字段相减语句与函数代码,举例说明了两个字段相减做法。

select a.字段1,字段2=a.字段2-isnull((select 字段2 from 表2 where a.字段1=字段1),0) from 表1 a

方法二

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)

本条技术文章来源于互联网,如果无意侵犯您的权益请点击此处反馈版权投诉

本文系统来源:php中文网

更多推荐

sql中两个表的某列相减_sql两个字段相减语句