<i id='OhcaR'><tr id='OhcaR'><dt id='OhcaR'><q id='OhcaR'><span id='OhcaR'><b id='OhcaR'><form id='OhcaR'><ins id='OhcaR'></ins><ul id='OhcaR'></ul><sub id='OhcaR'></sub></form><legend id='OhcaR'></legend><bdo id='OhcaR'><pre id='OhcaR'><center id='OhcaR'></center></pre></bdo></b><th id='OhcaR'></th></span></q></dt></tr></i><div id='OhcaR'><tfoot id='OhcaR'></tfoot><dl id='OhcaR'><fieldset id='OhcaR'></fieldset></dl></div>
        • <bdo id='OhcaR'></bdo><ul id='OhcaR'></ul>
        <tfoot id='OhcaR'></tfoot>

        <small id='OhcaR'></small><noframes id='OhcaR'>

        <legend id='OhcaR'><style id='OhcaR'><dir id='OhcaR'><q id='OhcaR'></q></dir></style></legend>

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

        时间:2023-08-19
      1. <tfoot id='dLvT3'></tfoot>

              <tbody id='dLvT3'></tbody>

              <legend id='dLvT3'><style id='dLvT3'><dir id='dLvT3'><q id='dLvT3'></q></dir></style></legend>

              <i id='dLvT3'><tr id='dLvT3'><dt id='dLvT3'><q id='dLvT3'><span id='dLvT3'><b id='dLvT3'><form id='dLvT3'><ins id='dLvT3'></ins><ul id='dLvT3'></ul><sub id='dLvT3'></sub></form><legend id='dLvT3'></legend><bdo id='dLvT3'><pre id='dLvT3'><center id='dLvT3'></center></pre></bdo></b><th id='dLvT3'></th></span></q></dt></tr></i><div id='dLvT3'><tfoot id='dLvT3'></tfoot><dl id='dLvT3'><fieldset id='dLvT3'></fieldset></dl></div>
                <bdo id='dLvT3'></bdo><ul id='dLvT3'></ul>

                  <small id='dLvT3'></small><noframes id='dLvT3'>

                  本文介绍了MySQL 错误 1093 - 无法在 FROM 子句中指定更新的目标表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我的数据库中有一个表 story_category,其中包含损坏的条目.下一个查询返回损坏的条目:

                  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);
                  

                  我试图删除它们执行:

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

                  但我收到下一个错误:

                  #1093 - 不能在 FROM 子句中为更新指定目标表story_category"

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

                  我怎样才能克服这个问题?

                  How can I overcome this?

                  推荐答案

                  更新:此答案涵盖一般错误分类.有关如何最好地处理 OP 的确切查询的更具体答案,请参阅此问题的其他答案

                  在 MySQL 中,您不能修改在 SELECT 部分使用的同一个表.
                  此行为记录在:http://dev.mysql.com/doc/refman/5.6/en/update.html

                  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

                  也许您可以将表格加入到自己的表格中

                  如果逻辑足够简单,可以重新调整查询,则丢失子查询并将表连接到自身,采用适当的选择标准.这将导致 MySQL 将表视为两种不同的事物,从而允许进行破坏性更改.

                  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
                  

                  或者,尝试将子查询更深入地嵌套到 from 子句中...

                  如果您绝对需要子查询,有一个解决方法,但它是丑陋的原因有很多,包括性能:

                  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);
                  

                  FROM 子句中的嵌套子查询创建了一个隐式临时表,因此它不算作您正在更新的同一个表.

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

                  ...但要注意查询优化器

                  但是,请注意 MySQL5.7.6 及以后,优化器可能会优化出子查询,但仍然会出现错误.幸运的是,optimizer_switch 变量可用于关闭此行为;尽管我不建议将其作为短期解决方案或一次性小任务.

                  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';
                  

                  感谢 Peter V. Mørch 在评论中提供的建议.

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

                  示例技术来自 Baron Schwartz,最初发表于 Nabble,在此处进行了释义和扩展.

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

                  这篇关于MySQL 错误 1093 - 无法在 FROM 子句中指定更新的目标表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:MySQL 将行转换为动态列数 下一篇:如何在 MySQL 中进行完全外部联接?

                  相关文章

                  最新文章

                    <bdo id='5zEO1'></bdo><ul id='5zEO1'></ul>

                • <small id='5zEO1'></small><noframes id='5zEO1'>

                  <i id='5zEO1'><tr id='5zEO1'><dt id='5zEO1'><q id='5zEO1'><span id='5zEO1'><b id='5zEO1'><form id='5zEO1'><ins id='5zEO1'></ins><ul id='5zEO1'></ul><sub id='5zEO1'></sub></form><legend id='5zEO1'></legend><bdo id='5zEO1'><pre id='5zEO1'><center id='5zEO1'></center></pre></bdo></b><th id='5zEO1'></th></span></q></dt></tr></i><div id='5zEO1'><tfoot id='5zEO1'></tfoot><dl id='5zEO1'><fieldset id='5zEO1'></fieldset></dl></div>
                  <tfoot id='5zEO1'></tfoot>

                    <legend id='5zEO1'><style id='5zEO1'><dir id='5zEO1'><q id='5zEO1'></q></dir></style></legend>