有一,求SQL句
在有T1(tid,tdate,tmoney)和T2(sid,sdate,edate)表,如下
表T1 | 表T2
tid tdate tmoney | sid sdate edate SS
001 2007/07/01 100 | 001 2007/07/01 2007/07/01 20
001 2007/07/02 80 | 001 2007/07/05 2007/07/06 5
.. .. .. | .. .. .. ..
002 2007/07/01 75 | 002 2007/07/08 2007/07/10 45
002 2007/07/02 25 | 003 2007/07/02 2007/07/05 10
.. .. .. | .. .. ..
在我想修改表T1的tmoney字段
如要T1.tmoney> 100-T2.SS 取100-T2.SS 否就保持T1.tmoney值不
件是T1表中的tid要等於T2表中的sid,且T1表中的tdate要在T2表的sdate和edate之.如何成SQL句.
[解决办法]
update T1
set tmoney=case when T1.tmoney> 100-T2.SS then 100-T2.SS else T1.tmoney end
from T1,T2
where T1.tid =T2.Sid
and T1.tdate between T2.sdate and T2.edate
[解决办法]
update T1
set tmoney = case when T1.tmoney > 100 - T2.SS then 100 - T2.SS else T1.money end
from T1, T2
where T1.tid = T2.sid and (T1.tdate between T2.sdate and T2.edate)
[解决办法]
Update
T1
Set
tmoney = (Case When T1.tmoney> 100-T2.SS Then 100-T2.SS Else T1.tmoney End)
From
T1
Inner Join
T2
On T1.tid = T2.sid And T1.tdate Between T2.sdate And T2.edate