简体   繁体   English

在 Oracle 中更新时使用内部联接

[英]Using inner join when updating in Oracle

I have 2 tables: I want to update my table1 records with the suitable age, which can be found in table2.我有 2 个表:我想用合适的年龄更新我的 table1 记录,可以在 table2 中找到。 Unique identifier is the BVD_ID_NUMBER.唯一标识符是 BVD_ID_NUMBER。 I tried to do this using the following code我尝试使用以下代码执行此操作

UPDATE table1
  SET table1.age = 
  (select table2.age2
    from 
      (select distinct table2.BVD_ID_NUMBER, table2.age2
        FROM table1
       inner JOIN table2
        on table1.ACQUIROR_BVD_ID_NUMBER=table2.BVD_ID_NUMBER)
   where table2.BVD_ID_NUMBER=table1.ACQUIROR_BVD_ID_NUMBER);

I received the following error: SQL Error: ORA-00904: "ORBIS_DISTINCT"."BVD_ID_NUMBER": invalid identifier 00904. 00000 - "%s: invalid identifier"我收到以下错误: SQL 错误:ORA-00904:“ORBIS_DISTINCT”。“BVD_ID_NUMBER”:无效标识符 00904。00000 - “%s:无效标识符”

Any help?有什么帮助吗?

Hmmm.嗯。 You have overcomplicated your query.您的查询过于复杂。 Usually, when using correlated subqueries, there is no reason to mention the outer table in the inner subquery.通常,当使用相关子查询时,没有理由在内子查询中提及外表。 Oracle doesn't allow scoping beyond one level for correlated subqueries, so you need to simplify for the correlation clause: Oracle 不允许关联子查询超过一层,因此您需要简化相关子句:

UPDATE table1 t1
    SET age = (select t2.age2
               from table2 t2
               where t1.ACQUIROR_BVD_ID_NUMBER = t2.BVD_ID_NUMBER
              );

This is likely to cause a "subquery returns more than one row" type of error.这很可能会导致“子查询返回多于一行”类型的错误。 To fix that, use aggregation or rownum = 1 :要解决这个问题,请使用聚合或rownum = 1

UPDATE table1 t1
    SET age = (select t2.age2
               from table2 t2
               where t1.ACQUIROR_BVD_ID_NUMBER = t2.BVD_ID_NUMBER and
                     rownum = 1
              );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM