mysql - SQL Update with join - 无法在 from 子句中指定 TABLE_NAME

标签 mysql sql sql-update

我遇到了无法解决的 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

最佳答案

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

您需要遵循以下语法:

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"

关于mysql - SQL Update with join - 无法在 from 子句中指定 TABLE_NAME,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10052939/

相关文章:

java - 如何只插入连接表而不插入 JPA Hibernate 中的引用表

MySQL Workbench 创建表时出现错误 1064

mysql - 某些 SQL Case 语句设置为 NULL

sql-server - 如何在OPENQUERY中使用update和join

sql - 更新 ID 匹配的数据和用例以选择哪些数据

java - Hibernate 映射 MySQL SET 类型

mysql - 添加一组 SQL 的总和

mysql - 如何理解这个包含 JOIN 的 MySQL 查询?

mysql - SQL 更新具有同一表中的行但具有不同条件的表

mysql - 如何重写SQL查询以在MariaDB和MySQL上实现相同的性能