2012-11-02 35 views
1

我试图执行以下查询:不能指定更新目标表中FROM子句

update table3 d set status = 'Complete' 
where d.id in 
(
    select b.id from table1 a, table3 b, table2 c 
    where a.id = b.table1_id 
    and c.id = b.table2_id 
    and c.examId = 16637     -- will be passed in by user 
    and a.id in (46,47,48,49)   -- will be passed in by user 
); 

所以,我试图更新的table3多行。

table3table1table2之间的连接表。

+0

您所查询的只是正常工作。有什么问题? – nawfal

回答

1

将它包装在一个子查询中(,从而为结果创建临时表)。我也建议使用ANSI SQL-92格式。

update table3 d 
set status = 'Complete' 
where d.id in 
(
    SELECT ID 
    FROM 
    (
     select b.id 
     from table1 a 
       INNER JOIN table3 b 
        ON a.id = b.table1_id 
       INNER JOIN table2 c 
        ON c.id = b.table2_id 
     where c.examId = 16637 and 
       a.id in (46,47,48,49) 
    ) xx 
); 

或使用JOIN

update table3 d 
     INNER JOIN 
     (
      SELECT ID 
      FROM 
      (
       select b.id 
       from table1 a 
         INNER JOIN table3 b 
          ON a.id = b.table1_id 
         INNER JOIN table2 c 
          ON c.id = b.table2_id 
       where c.examId = 16637 and 
         a.id in (46,47,48,49) 
      ) xx 
     ) y ON d.id = y.id 
set status = 'Complete' 
+0

约翰,为什么他的初始查询不起作用?它看起来对我来说很好 – nawfal

+0

@nawfal,因为你不能定位你已经加入的同一个表。我会举一个例子。请等待 –

+0

哦,是的,我知道了。无需示例。谢谢:) – nawfal

相关问题