UPDATE forms SET
pos = (SELECT MIN(pos)-1 FROM forms)
WHERE id=$id
This doesn't work, error message:
**You can't specify target table 'form' for update in FROM clause**
I hope it's clear: I want to get the minimal element-1 from the same table and assign it to pos
Consp is right that it's not supported. There's a workaround, however:
A version that is probably faster:
I think that you can not use a subquery inside an update statement, but any way there are workarounds for it ...
Here is a Quotation from the following site:
"dev.mysql.com"
“Currently, you cannot delete from a table and select from the same table in a sub-query ”
Your problem is stated plainly in the MySQL manual:
You'll want to use a transaction. Turn AutoCommit off, begin a transaction, then do a SELECT MIN(pos)-1 FROM forms FOR UPDATE, take that result, do the update with it, then commit your transaction.
You could also try: