sql语句怎么写啊(100分)

  • 主题发起人 主题发起人 huangjiahan
  • 开始时间 开始时间
H

huangjiahan

Unregistered / Unconfirmed
GUEST, unregistred user!
A表:
id1 id2 date1 date2 num1 num2
001 001 20021001 20021202 10 0
B表:
id1 id2 date num
001 001 20021102 5
001 002 20021001 10

如果B表中满足以下2个条件:
1、B表的id1、id2分别和A表的id1、id2相同;
2、B表中的时间date介于A表的date1和date2之间;
则B表中的相应记录的num:=num-num2+num1;
这样的sql语句该怎么写?
 
update B,A Set B.num = B.num - A.num2 + A.num1
where B.id1 = A.id1 and B.id2 = A.id2
and B.date between (A.date1 and A.date2)
试试吧,不保证对啊!
 
update b set b.num=a.num1+a.num2
from a,b
where a.id1=b.id1 and a.id2=b.id2
and b.date between a.date1 and a.date2
 
则B表中的相应记录的num:=num-num2+num1:

不好意思,看错了
 
对对,应该是xj_liyq,的写法才正确!呵呵。。。
 
update B set num=num - num2 + num1 from A, B
where A.id1=B.id1 and A.id2=B.id2 and date between date1 and date2
 
update b set b.num=b.num-a.num2+a.num1 where b.id1=a.id1 and b.id2=a.id2
and b.date between a.date1 and a.date2
 
打错了。
update b set b.num=b.num-a.num2+a.num1 from a,b where b.id1=a.id1 and b.id2=a.id2
and b.date between a.date1 and a.date2
 
不对啊,提示操作符丢失啊
在from a,b的地方,怎么回事
 

Similar threads

H
回复
11
查看
178
Awen.ZW.Yang
A
H
回复
13
查看
88
maolu28
M
H
回复
13
查看
131
ldaan3
L
后退
顶部