sql oracle更新

sql oracle更新,sql,oracle,Sql,Oracle,我有这个SQL UPDATE table1 t1 SET (t1.wert) = (select t2.bezeichnung from table2 t2 where t1.id = t2.cpbezeichnung) where t1.id = t2.cpbezeichnung 。。。我不能运行,因为它告诉我它不知道第5行中的t2.cpbezeichung 如何修复它?只有在子查询中才能看到t2别名(以及表2)。我猜你想 UPDATE table1 t1 SET t1.wert

我有这个SQL

UPDATE table1 t1
SET (t1.wert) = 
(select t2.bezeichnung from table2 t2 
where t1.id = t2.cpbezeichnung) 
where t1.id = t2.cpbezeichnung
。。。我不能运行,因为它告诉我它不知道第5行中的
t2.cpbezeichung

如何修复它?

只有在子查询中才能看到
t2
别名(以及
表2
)。我猜你想

UPDATE table1 t1
  SET t1.wert = (select t2.bezeichnung 
                   from table2 t2 
                  where t1.id = t2.cpbezeichnung) 
where exists (select 1
                from table2 t2 
               where t1.id = t2.cpbezeichnung) 
它会更新两个表之间匹配的每一行。如果这不是您想要的,那么发布测试用例会很有帮助。

只有在子查询中才能看到
t2
别名(以及
table2
)。我猜你想

UPDATE table1 t1
  SET t1.wert = (select t2.bezeichnung 
                   from table2 t2 
                  where t1.id = t2.cpbezeichnung) 
where exists (select 1
                from table2 t2 
               where t1.id = t2.cpbezeichnung) 

它会更新两个表之间匹配的每一行。如果这不是您想要的,那么发布一个测试用例会很有帮助。

别名为t2的表没有为更新查询定义,所以第5行显然不知道它。表t2仅在第3行和第4行的子查询中定义

在第5行的条件下,您到底想要实现什么

若要防止在表t2中并没有适当记录的行中将NULL设置为t1.wert,则需要替换第5行的条件

UPDATE table1 t1
SET (t1.wert) = 
(select t2.bezeichnung from table2 t2 where t1.id = t2.cpbezeichnung) 
where t1.id IN (SELECT t2.cpbezeichnung from table2)

这将仅为t2中存在t1.id的记录设置t1.wert中的值。cpbezeichnung。

没有为更新查询定义别名为t2的表,因此在第5行显然不知道它。表t2仅在第3行和第4行的子查询中定义

在第5行的条件下,您到底想要实现什么

若要防止在表t2中并没有适当记录的行中将NULL设置为t1.wert,则需要替换第5行的条件

UPDATE table1 t1
SET (t1.wert) = 
(select t2.bezeichnung from table2 t2 where t1.id = t2.cpbezeichnung) 
where t1.id IN (SELECT t2.cpbezeichnung from table2)
这将仅为t2.cpbezeichnung中存在t1.id的记录设置t1.wert中的值。

使用时,不能在外部查询中使用内部查询中的别名

外部查询除了其结果外,对内部查询一无所知

使用时,不能在外部查询中使用来自内部查询的别名

外部查询除了其结果外,对内部查询一无所知


您必须在两个表上使用内部联接

差不多

UPDATE table1 t1 INNERJOIN table t2 ON your_table_condition SET t1.wert = (select t2.bezeichnung from t2 where t1.id = t2.cpbezeichnung) where t1.id = t2.cpbezeichnung

您必须在两个表上使用内部联接

差不多

UPDATE table1 t1 INNERJOIN table t2 ON your_table_condition SET t1.wert = (select t2.bezeichnung from t2 where t1.id = t2.cpbezeichnung) where t1.id = t2.cpbezeichnung