简体   繁体   English

MySQL-表两次指定为更新目标和单独数据源

[英]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.

相关问题 表被指定两次,既作为 &#39;UPDATE&#39; 的目标,也作为 mysql 中数据的单独源 - Table is specified twice, both as a target for 'UPDATE' and as a separate source for data in mysql 表名称指定两次作为更新目标和单独的数据源 - Table name specified twice both as a target for update and separate source for data 表名指定两次,既作为更新的目标,又作为单独的数据源 - Table name specified twice, both as a target for update and as a separate source for data 表被指定两次,既作为 &#39;UPDATE&#39; 的目标,也作为单独的数据源 - Table is specified twice, both as a target for 'UPDATE' and as a separate source for data 表被指定两次,既作为&#39;UPDATE&#39;的目标,又作为单独的数据源 - Table is specified twice, both as a target for 'UPDATE' and as a separate source for data 错误发生在另一个设置上,而不是在我的设置上:“表被指定两次,既作为&#39;UPDATE&#39;的目标,又作为MySQL中数据的单独源” - The error occurs on another setup, not on mine: “Table is specified twice, both as a target for 'UPDATE' and as a separate source for data in MySQL” 该表被两次指定为INSERT的目标和单独的数据源 - table is specified twice both as a target for INSERT and as separate source of data MYSQL 删除 - 表 &#39;USER_TABLE&#39; 被指定两次,既作为 &#39;DELETE&#39; 的目标,也作为单独的数据源 - MYSQL delete - Table 'USER_TABLE' is specified twice, both as a target for 'DELETE' and as a separate source for data 表&#39;tbl_name&#39;被指定两次,既作为&#39;UPDATE&#39;的目标,又作为数据的单独源 - Table 'tbl_name' is specified twice, both as a target for 'UPDATE' and as a separate source for data 错误: <table_name> 指定两次,既作为“ UPDATE”的目标,又作为单独的数据源 - Error : <table_name> specified twice, both as a target for 'UPDATE' and as a separate source for data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM