Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><em>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</em></p> <p>In MySQL, you can't modify the same table which you use in the SELECT part.<br> This behaviour is documented at: <a href="http://dev.mysql.com/doc/refman/5.6/en/update.html" rel="noreferrer">http://dev.mysql.com/doc/refman/5.6/en/update.html</a></p> <p><strong>Maybe you can just join the table to itself</strong></p> <p>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.</p> <pre><code>UPDATE tbl AS a INNER JOIN tbl AS b ON .... SET a.col = b.col </code></pre> <p><strong>Alternatively, try nesting the subquery deeper into a from clause ...</strong></p> <p>If you absolutely need the subquery, there's a workaround, but it's ugly for several reasons, including performance:</p> <pre><code>UPDATE tbl SET col = ( SELECT ... FROM (SELECT.... FROM) AS x); </code></pre> <p>The nested subquery in the FROM clause creates an <em>implicit temporary table</em>, so it doesn't count as the same table you're updating. </p> <p><strong>... but watch out for the query optimiser</strong></p> <p>However, beware that from <a href="http://dev.mysql.com/doc/relnotes/mysql/5.7/en/news-5-7-6.html" rel="noreferrer">MySQL 5.7.6</a> and onward, the optimiser may optimise out the subquery, and still give you the error. Luckily, the <code>optimizer_switch</code> 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.</p> <pre><code>SET optimizer_switch = 'derived_merge=off'; </code></pre> <p><em>Thanks to <a href="https://stackoverflow.com/users/345716/peter-v-m%C3%B8rch">Peter V. Mørch</a> for this advice in the comments.</em></p> <p>Example technique was from Baron Schwartz, <a href="http://grokbase.com/t/mysql/mysql/08259dm24b/error-you-cant-specify-target-table-for-update-in-from-clause" rel="noreferrer">originally published at Nabble</a>, paraphrased and extended here.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload