larry*wei 2008-09-05 10:03 采纳率: 0%
浏览 594
已采纳

Mysql 错误1093-无法在 FROM 子句中指定更新的目标表

I have a table story_category in my database with corrupt entries. The next query returns the corrupt entries:

SELECT * 
FROM  story_category 
WHERE category_id NOT IN (
    SELECT DISTINCT category.id 
    FROM category INNER JOIN 
       story_category ON category_id=category.id);

I tried to delete them executing:

DELETE FROM story_category 
WHERE category_id NOT IN (
    SELECT DISTINCT category.id 
    FROM category 
      INNER JOIN story_category ON category_id=category.id);

But I get the next error:

#1093 - You can't specify target table 'story_category' for update in FROM clause

How can I overcome this?

转载于:https://stackoverflow.com/questions/45494/mysql-error-1093-cant-specify-target-table-for-update-in-from-clause

  • 写回答

14条回答 默认 最新

  • hurriedly% 2008-09-05 10:07
    关注

    Update: This answer covers the general error classification. For a more specific answer about how to best handle the OP's exact query, please see other answers to this question

    In MySQL, you can't modify the same table which you use in the SELECT part.
    This behaviour is documented at: http://dev.mysql.com/doc/refman/5.6/en/update.html

    Maybe you can just join the table to itself

    If the logic is simple enough to re-shape the query, lose the subquery and join the table to itself, employing appropriate selection criteria. This will cause MySQL to see the table as two different things, allowing destructive changes to go ahead.

    UPDATE tbl AS a
    INNER JOIN tbl AS b ON ....
    SET a.col = b.col
    

    Alternatively, try nesting the subquery deeper into a from clause ...

    If you absolutely need the subquery, there's a workaround, but it's ugly for several reasons, including performance:

    UPDATE tbl SET col = (
      SELECT ... FROM (SELECT.... FROM) AS x);
    

    The nested subquery in the FROM clause creates an implicit temporary table, so it doesn't count as the same table you're updating.

    ... but watch out for the query optimiser

    However, beware that from MySQL 5.7.6 and onward, the optimiser may optimise out the subquery, and still give you the error. Luckily, the optimizer_switch variable can be used to switch off this behaviour; although I couldn't recommend doing this as anything more than a short term fix, or for small one-off tasks.

    SET optimizer_switch = 'derived_merge=off';
    

    Thanks to Peter V. Mørch for this advice in the comments.

    Example technique was from Baron Schwartz, originally published at Nabble, paraphrased and extended here.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(13条)

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)