Mysql SELECT inside UPDATE

前端 未结 4 1829
UPDATE forms SET

pos = (SELECT MIN(pos)-1 FROM forms)

WHERE id=$id

This doesn\'t work, error message:

**You can\'t specify target         


        
4条回答
  •  一个人的身影
    2020-12-31 20:32

    Your problem is stated plainly in the MySQL manual:

    Currently, you cannot update a table and select from the same table in a subquery.

    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.

提交回复
热议问题