2015-05-07 94 views
1

我有这样的SQL ...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.cpbezeichnung

我该如何解决?

回答

3

别名为T2表没有为UPDATE查询定义,所以它显然并不在行知5.表T2仅被定义里面的子查询上线3和4

究竟是你想实现与条件在线5?

如果你想阻止设置为NULL到t1.wert的行那里是在表t2没有相应的记录,那么你需要在第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) 

这将在T1设置的值。 wert只适用于t2.cpbezeichnung中存在t1.id的记录。

+0

感谢的人!我在那里犯了一个愚蠢的错误 – piguy

1

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) 

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

0

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

外部查询对内部查询一无所知,除了它的结果。

Ref

0

你将不得不使用一个内部的两个表的连接。

喜欢的东西

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