1

我有一个我无法解决的 sql 错误。我必须用几个连接更新一个表。我尝试在没有“in”子句的情况下这样做,但这没有用。现在使用 in 子句,这是我的查询:

 UPDATE sc_module_architect 
    SET
    item_name="Print Cover Price"
    WHERE 
    item_id IN (
         SELECT a.item_id
         FROM sc_module_architect a
         LEFT JOIN sc_module_architect_category_links l on l.item_id=a.item_id
         LEFT JOIN sc_module_architect_category c on c.category_content_id=l.content_id
         LEFT JOIN sc_content sc on sc.content_id=l.content_id
         WHERE item_active=1
         AND content_name LIKE "ed_abc_print%"
         LIKE item_name LIKE "Cover Price%");

现在运行它时出现错误

/* SQL Error (1093): You can't specify target table 'sc_module_architect' for update in FROM clause */

无法弄清楚为什么会出现这种情况。有任何想法吗?mysql 5.5

4

4 回答 4

2

您也不能同时在子查询中使用更新下的表:

您需要遵循以下语法:

update tablea JOIN tableb ..... SET columnname='yourvalue' where [....]

试试下面:

update sc_module_architect as t1  LEFT JOIN  (select a.item_id
    from sc_module_architect a
    left join sc_module_architect_category_links l on l.item_id=a.item_id
    left join sc_module_architect_category c on c.category_content_id=l.content_id
    left join sc_content sc on sc.content_id=l.content_id
    where
    item_active=1
    and content_name like "ed_abc_print%"
    and item_name like "Cover Price%") as t2
    on t1.item_id=t2.itemid
    set
    t1.item_name="Print Cover Price"
于 2012-04-07T08:52:39.230 回答
1

将条件从WHERE移到 a JOIN。(而且这些left连接似乎不合适。我认为它们应该是INNER连接):

UPDATE sc_module_architect AS ma
  JOIN
     ( SELECT a.item_id
         from sc_module_architect a
           left join sc_module_architect_category_links l 
             on l.item_id=a.item_id
           left join sc_module_architect_category c 
             on c.category_content_id=l.content_id
           left join sc_content sc on sc.content_id=l.content_id
       WHERE content_name LIKE 'ed_abc_print%'
     ) AS t
    ON t.item_id = ma.item_id 

  SET
    ma.item_name = 'Print Cover Price'

WHERE ma.item_active = 1
  AND ma.item_name LIKE 'Cover Price%' ;
于 2012-04-07T08:51:40.987 回答
1

您可以先将所有 Id 放入一个临时表中,然后将其用作插入中的查找

CREATE TEMPORARY TABLE tempTable (item_id INT);

INSERT INTO tempTable (item_id)
SELECT a.item_id
FROM sc_module_architect a
LEFT JOIN sc_module_architect_category_links l on l.item_id=a.item_id
LEFT JOIN sc_module_architect_category c on c.category_content_id=l.content_id
LEFT JOIN sc_content sc on sc.content_id=l.content_id
WHERE
  item_active=1
  AND content_name like "ed_abc_print%"
  AND item_name like "Cover Price%";

UPDATE sc_module_architect 
SET
  item_name="Print Cover Price"
WHERE 
  item_id in (select item_id from tempTable)
于 2012-04-07T08:31:55.817 回答
1

当您在子查询中将表 sc_module_architect 设置为“a”时,请使用它

update a 
set
item_name="Print Cover Price"
WHERE 
item_id in (select a.item_id
from sc_module_architect a
left join sc_module_architect_category_links l on l.item_id=a.item_id
left join sc_module_architect_category c on c.category_content_id=l.content_id
left join sc_content sc on sc.content_id=l.content_id
where
item_active=1
and content_name like "ed_abc_print%"
and item_name like "Cover Price%");
于 2012-04-07T08:46:07.667 回答