日期:2014-05-17 浏览次数:20837 次
update tab1 B set b=b-(select max(b) from tab1 A where A.b=A.c and A.a=010400) , c=c-(select max(c) from tab1 A where A.b=A.c and A.a=010400) where a=010000; update tab1 B set b=b-(select max(b) from tab1 A where A.b=A.c and A.a=010400) , c=c-(select max(c) from tab1 A where A.b=A.c and A.a=010400) where a=000000; update tab1 B set b=0 ,c=0 where a=000000 and b=c;
------解决方案--------------------
实测数据
CREATE TABLE T134 ( a VARCHAR2(20), b NUMBER(4), c NUMBER(4), d VARCHAR2(20) ); INSERT INTO T134 VALUES('000000', 20, 20, 'a'); INSERT INTO T134 VALUES('010000', 15, 15, 'a'); INSERT INTO T134 VALUES('010400', 5, 5, 'a'); INSERT INTO T134 VALUES('000000', 25, 25, 'b'); INSERT INTO T134 VALUES('010000', 15, 15, 'b'); INSERT INTO T134 VALUES('010400', 5, 5, 'b'); INSERT INTO T134 VALUES('000000', 20, 20, 'c'); INSERT INTO T134 VALUES('010000', 15, 15, 'c'); INSERT INTO T134 VALUES('010400', 5, 0, 'c'); -- 更新a = '0000000' 和a='010000'的行 UPDATE T134 T1 SET b = b - (SELECT b FROM T134 T2 WHERE b = c AND a = '010400' AND T2.d = T1.d), c = c - (SELECT c FROM T134 T3 WHERE b = c AND a = '010400' AND T3.d = T1.d) WHERE (a = '000000' OR a = '010000') AND EXISTS(SELECT 1 FROM T134 T4 WHERE b = c AND a = '010400' AND T4.d = T1.d) -- 更新 a = '010400'的行 UPDATE T134 SET b = 0, c = 0 WHERE b = c AND a = '010400';