Oracle数据库,将数据从1table更新到另一个

Oracle database, update data from 1 table to another

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

Table 1(标志模板等):
et.emblemtemplate_id、et.customer_id、et.code

Table 2(PRODUCTISTPERCUSTOMER):
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
  4. 加入 table

我创建了一个 select:

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

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

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;

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;