【问题标题】:Oracle database, update data from 1 table to anotherOracle数据库,将数据从一张表更新到另一张表
【发布时间】:2020-12-22 15:02:37
【问题描述】:

我在 oracle 12c 数据库中有 2 个表,我想从 1 个表更新到下一个。

表 1(EMBLEMTEMPLATE 等):
et.emblemtemplate_id, et.customer_id, et.code

表 2(产品列表PERCUSTOMER):
plpc.productlistpercustomer_id, plpc.customer_id, plpc.emblemtemplate_id,

我想做什么:

  1. 检查所有 plpc.productlistpercustomer 是否填写了 plpc.emblemtemplate_id,如果没有则需要更新。
  2. plpc.emblemtemplate_id 需要用 et.emblemtemplate_id 更新,其中 et.code = "999991"
  3. 这些表必须在 CUSTOMER_ID 上连接

我创建了一个选择:

select plpc.customer_id,
  plpc.productlistpercustomer_id,
  plpc.emblemtemplate_id,
  et.customer_id,
  et.emblemtemplate_id,
  et.code
  from productlistpercustomer plpc
  inner join emblemtemplate et
  on et.customer_id = plpc.customer_id
  where et.code = '999991'

谁能帮我把它翻译成一个 sql 更新脚本?

谢谢!

Visual of what i want to do

【问题讨论】:

    标签: sql sql-update oracle12c


    【解决方案1】:

    您可以尝试使用带有相关子查询的更新:

    UPDATE
        productlistpercustomer plpc
    SET
        emblemtemplate_id = (SELECT et.emblemtemplate_id
                             FROM emblemtemplate et
                             WHERE et.customer_id = plpc.customer_id AND
                                   et.code = '999991')
    WHERE
        emblemtemplate_id IS NULL;
    

    【讨论】:

    • 这太完美了!
    【解决方案2】:

    merge怎么样?

    MERGE INTO productlistpercustomer p
         USING emblemtemplate e
            ON (e.customer_id = p.customer_id)
    WHEN MATCHED
    THEN
       UPDATE SET p.emblemtemplate_id = p.emblemtemplate_id
               WHERE     e.code = 999991
                     AND p.emblemtemplate_id IS NULL;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多