Skip to content

Instantly share code, notes, and snippets.

@kurko
Created April 4, 2011 20:44
Show Gist options
  • Save kurko/902390 to your computer and use it in GitHub Desktop.
Save kurko/902390 to your computer and use it in GitHub Desktop.
Let's say we want to calculate something, i.e. increase a counter
# this doesn't work
"INSERT INTO table1 (field1) VALUE( (SELECT field1 FROM table1 LIMIT 1)+1 )"
# "You can't specify target table 'table1' for update in FROM clause"
# but this works
"INSERT INTO table1 (field1) VALUE( (SELECT field1 FROM (SELECT field1 FROM table1 LIMIT 1))+1 )"
# the FROM statement becomes just a repetition of the subquery.
# Thus, MySQL creates the temporary table with no problem.
# I understand this change isolates the INSERT's table1 from the SELECT's,
# so MySQL doesn't think it's the same, but isn't this funny, redundant and tricky?
@kurko
Copy link
Author

kurko commented Apr 4, 2011

This applies to UPDATE as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment