Mysql更新子查询指定目标表
我在通过选择表中已有的最高 Final_id 并添加 +1 来更新 Final_id 时遇到问题。
下面的查询输出错误:“您无法在 FROM 子句中指定用于更新的目标表‘customer_orders’”,遗憾的是我不明白为什么......
UPDATE customer_orders
SET final_id = (SELECT final_id FROM customer_orders ORDER BY final_id DESC)+1,
status = 2,
payment_id = '{$transaction_id}',
payment_type = '{$type}',
payment_reserved = '{$amount}',
payment_currency = '{$cur}',
payment_cardnopostfix = '{$postfix}',
payment_fraud_suspicious = '{$fraud}'
WHERE id = '{$order_id}'
我正在尝试为系统中的最终订单设置唯一的递增 ID。
我希望有人能告诉我我做错了什么!
此致
I'm having trouble with updating final_id by selecting the highest final_id already in table and adding +1.
The query below outputs the error: "You can't specify target table 'customer_orders' for update in FROM clause" and I sadly fail to see why..
UPDATE customer_orders
SET final_id = (SELECT final_id FROM customer_orders ORDER BY final_id DESC)+1,
status = 2,
payment_id = '{$transaction_id}',
payment_type = '{$type}',
payment_reserved = '{$amount}',
payment_currency = '{$cur}',
payment_cardnopostfix = '{$postfix}',
payment_fraud_suspicious = '{$fraud}'
WHERE id = '{$order_id}'
I'm trying to set a unique increasing ID for finalized orders in my system.
I hope someone can tell me what I'm doing wrong!
Best regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以重写查询并使用 join
You can rewrite your query and use join
将内部查询更改为 SELECT max(final_id) FROM customer_orders
change the inner query to
SELECT max(final_id) FROM customer_orders
试试这个:
Try this: