【问题标题】:can't specify target table for UPDATE in FROM clause无法在 FROM 子句中为 UPDATE 指定目标表
【发布时间】:2012-11-02 02:46:18
【问题描述】:

我正在尝试执行以下查询:

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 之间的连接表。

【问题讨论】:

  • 您的查询工作正常。有什么问题?

标签: mysql sql


【解决方案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'

【讨论】:

猜你喜欢
  • 2016-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-17
  • 2015-07-10
  • 2014-07-19
  • 1970-01-01
相关资源
最近更新 更多