使用 Oracle 连接两个表更新列

Update column by join two tables using Oracle

我需要更新 table 列与其他 table 的连接 SQL 我的伪代码如下:

 update table1 T1 set T1.user = T2.product where T2.product like '%P12%' 
 and T1.order = T2.order;

我也尝试过使用 JOIN 查询,但使用 Oracle 时出现错误“SQL 命令未正确结束”。

在 Oracle 中,您可以使用相关子查询:

update table1 t1
set t1.user = (select t2.product from table2 t2 where t1.order = t2.order and t2.product like '%P12%' )
where exists(select 1 from table2 t2 where t1.order = t2.order and t2.product like '%P12%')

我很惊讶有人会从一个叫 product 的东西更新一个叫 user 的东西——但这坚持你原来的伪代码。

在 Oracle 中,不允许使用 JOIN 更新。

您可以使用 MERGE。它可以使用另一个 table (s).

更新一个 table
MERGE INTO T1
USING (SELECT T2.PRODUCT,
              T2.ORDER
         FROM T2 WHERE T2.PRODUCT LIKE '%P12%') T2 
ON ( T1.ORDER = T2.ORDER )
WHEN MATCHED THEN UPDATE
   SET T1.USER = T2.PRODUCT