【问题标题】:How to update from other table in Oracle's sql如何从 Oracle sql 中的另一个表更新
【发布时间】:2019-08-03 13:14:09
【问题描述】:

我有两个表,我想从 Oracle 的 sql 中的表 B 更新表 A

table A
customer_id    geo_id     geo
1234567890       3521     texas
0987654321       3624     dallas
1597536842       3121     mexicocity
table B
geo_id        customer_id
8745          1234567890
2145          0987654321
3699          1597536842
update table A
set   geo_id   = (select geo_id from table B)
where tableA.customer_id = tableB.customer_id;

【问题讨论】:

    标签: sql oracle sql-merge


    【解决方案1】:

    使用MERGE 声明

    MERGE INTO tablea a 
         using tableb b ON( a.customer_id = b.customer_id ) 
    WHEN matched THEN 
      UPDATE SET a.geo_id = b.geo_id 
    

    或相关更新

    update tablea a set 
        a.geo_id = (select geo_id from 
                          tableb b 
                          where a.customer_id = b.customer_id)
    

    DEMO

    【讨论】:

    • 谢谢,但结果是“单行子查询返回多行”
    • @MohammadAminKaviani :您必须使用正确的键来唯一标识记录。似乎唯一的 customer_id 条件不是唯一的
    猜你喜欢
    • 1970-01-01
    • 2023-03-08
    • 2018-07-01
    • 1970-01-01
    • 2011-10-25
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    相关资源
    最近更新 更多