sql 两个字段相减语句

WBOY
Release: 2016-06-07 17:47:54
Original
9616 people have browsed it

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)

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!