我有两张桌子
->DESC TABLE1
SC_CD VARCHAR2(2)
DOC_AMT NUMBER(10,2)
DEPOSIT_BANK VARCHAR2(2)
-> DESC TABLE2
SC_CD VARCHAR2(2)
DOC_AMT NUMBER(10,2)
DEPOSIT_BANK VARCHAR2(2)
->SELECT * FROM TABLE1;
SC DOC_AMT DE
-- --------- --
78350 70
15840 70
3750 70
->SELECT * FROM TABLE2;
SC DOC_AMT DE
-- --------- --
1 78350 70
6 840 80
3 3750 70
现在,我想更新TABLE2 SC_CD
中的TABLE1 SC_CD更新后,我想要这个结果......
->SELECT * FROM TABLE1;
SC DOC_AMT DE
-- --------- --
1 78350 70
15840 70
3 3750 70
谢谢
答案 0 :(得分:1)
你可以试试这个
SQL Server
update TABLE1
set SC_CD = a.SC_CD
from TABLE2 a
where DOC_AMT = a.DOC_AMT and DEPOSIT_BANK = a.DEPOSIT_BANK
ORACLE
update TABLE1
set SC_CD = (SELECT SC_CD
FROM TABLE2
WHERE TABLE1.DOC_AMT = TABLE2.DOC_AMT
AND TABLE1 .DEPOSIT_BANK = TABLE2.DEPOSIT_BANK)
答案是更新
答案 1 :(得分:0)
针对此类问题的最佳解决方案是MERGE
语句,如下所示:
merge into table1 t1
using table2 t2
on ( t1.doc_amt = t2.doc_amt
and t1.deposit_bank = t2.deposit_bank)
when matched then update
set t1.sc_cd = t2.sc_cd;
https://docs.oracle.com/cd/B28359_01/server.111/b28286/statements_9016.htm