[英]MySQL - table specified twice both as a target for update and as a separate source for data
When I try to execute these two queries: 当我尝试执行以下两个查询时:
UPDATE bills
SET id_bill_tmp = (SELECT IFNULL(id_bill_tmp, 0)+1 AS id_bill_tmp FROM bills)
WHERE id_interess = 1;
UPDATE bills
SET id_bill_tmp = (SELECT max(id_bill_tmp)+1 FROM bills)
WHERE id_interess = 1;
I get: 我得到:
table 'bills' is specified twice both as a target for 'update' and as a separate source for data
表“ bills”被两次指定为“更新”的目标和单独的数据源
How can I rewrite UPDATE
to not report this error? 如何重写
UPDATE
以不报告此错误?
You cant update the table by getting the data from the same table in a single query. 您无法通过在单个查询中从同一表中获取数据来更新表。 For this at least you should use a temporary table or else you should go with a view
为此,至少您应该使用一个临时表,否则应该使用一个视图
Create a view with your select query 使用选择查询创建视图
CREATE VIEW `view_name` AS SELECT IFNULL(id_bill_tmp, 0)+1 AS id_bill_tmp FROM bills
and then update the table using the view 然后使用视图更新表
UPDATE bills
SET id_bill_tmp = (SELECT id_bill_tmp FROM view_name)
WHERE id_interess = 1;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.