Mysql SELECT 里面的 UPDATE
UPDATE forms SET
pos = (SELECT MIN(pos)-1 FROM forms)
WHERE id=$id
这不起作用,错误消息:
**You can't specify target table 'form' for update in FROM clause**
我希望很清楚:我想从同一个表中获取最小的 element-1 并将其分配给 pos
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Consp 是对的,它不受支持。不过,有一个解决方法:
一个可能更快的版本:
Consp is right that it's not supported. There's a workaround, however:
A version that is probably faster:
您的问题在 MySQL 手册中已明确说明:
您将需要使用交易。关闭自动提交,开始事务,然后执行 SELECT MIN(pos)-1 FROM forms FOR UPDATE,获取该结果,用它进行更新,然后提交事务。
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:
我认为您不能在更新语句中使用子查询,但无论如何都有解决方法......
这是来自以下站点的引用:
"dev.mysql.com"
“目前,您不能在子查询中从表中删除并从同一个表中进行选择”
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 ”