mysql错误代码1093:不能在from子句中指定更新的目标表

4ktjp1zp  于 2021-06-21  发布在  Mysql
关注(0)|答案(3)|浏览(433)

我可能做错了什么,我以前搜索过它,我发现一些解决方法,告诉我这在mysql上是不可能的,其他人发布说,这是由于mysql优化器,所以你可以简单地关闭它并继续,但它不工作,我。
我只想要一个简单的解决方法来处理这个问题。我唯一能找到id\u address\u delivery和id\u address\u invoice的表来自orders表,该数据也在其他表中,但可以为空,因此无法在其他位置搜索它。

SET optimizer_switch = 'derived_merge=off';

update orders 
set id_customer = (select id_customer from customer where email like 'foo@foo.com'),
id_address_delivery = (select id_address_delivery from orders where id_customer = (select id_customer from customer where email like 'foo@foo.com') LIMIT 1),
id_address_invoice = (select id_address_invoice from orders where id_customer = (select id_customer from customer where email like 'foo@foo.com') LIMIT 1)
where id_customer = (select id_customer from customer where email like 'foo2@foo2.com');

我得到错误代码:1093。您不能在mysql工作台上为updatein from子句指定目标表'orders',即使我是appply

SET optimizer_switch = 'derived_merge=off';

有什么办法来处理这种情况吗?我感谢您设置了一些变量,例如:

SET @iad = (select id_address_delivery from orders where id_customer = (select id_customer from customer where email like 'foo@foo.com') LIMIT 1);

然后将此var设置为如下值:

id_address_delivery = @iad;

我没有得到一个错误响应时,这样做,但它真的持续了很多(我完全不知道为什么)和tiemout消息出现(30秒),我试着把它放在120秒,并得到相同的超时消息。
编辑:
我试过用alias,但没有结果。相同错误:

update orders AS sor
set sor.id_customer = (select id_customer from customer where email like 'foo@foo.com'),
sor.id_address_delivery = (select a.id_address_delivery from orders as a where a.id_customer = (select id_customer from customer where email like 'foo@foo.com') LIMIT 1),
sor.id_address_invoice = (select b.id_address_invoice from orders as b where b.id_customer = (select id_customer from customer where email like 'foo@foo.com') LIMIT 1)
where pso.id_customer = (select id_customer from customer where email like 'foo2@foo2.com');

/编辑:
我读了一些贴有相同问题标签的帖子,但是在那里我找到了一些其他条款的解决方法,我不知道如何将它们应用到我的特定案例中。
我该怎么办?谢谢。

gopyfrb3

gopyfrb31#

另一个答案是,如果要对表执行更新/插入/删除操作,则不能在内部查询中引用该表(但是可以引用外部表中的字段…)
检查以下与您的问题相同的问题并解决它们。
mysql错误1093-无法在from子句中指定更新的目标表
不能在from子句中指定更新的目标表

x4shl7ld

x4shl7ld2#

create view foo_data as ( select o.id_customer,o.id_address_delivery,o.id_address_invoice from  orders as o inner join customer as c on o.id_customer = c.id_customer where c.email like 'foo@foo.com' limit 1)    

update orders as o inner join foo_data as f on f.id_customer = o.id_customer
set optimizer_switch = 'derived_merge=off',
set o.id_customer = f.id_customer,
set o.id_address_delivery  = f.id_address_delivery ,
set o.id_address_invoice = f.id_address_invoice,
where o.email email like 'foo2@foo2.com';

drop view foo_data;

Please try using creating temp view
0h4hbjxa

0h4hbjxa3#

我终于处理了它设置变量,仍然不知道为什么这导致了超时之前。我在where子句上添加了null保护,以便在某些var init失败时不插入null值。
有一份我的工作示例供您参考:

use dbname;

SET SQL_SAFE_UPDATES = 0;
SET optimizer_switch = 'derived_merge=off';

# -- data asociated with wrong@foomail.com will be associated to correct@foomail.com

SET @delad = (SELECT id_address_delivery FROM orders WHERE customer = (SELECT id_customer FROM customer WHERE email LIKE 'correct@foomail.com') LIMIT 1);
SET @delin = (SELECT id_address_invoice FROM orders WHERE id_customer = (SELECT id_customer FROM customer WHERE email LIKE 'correct@foomail.com') LIMIT 1);
SET @uid = (SELECT id_customer FROM customer WHERE email LIKE 'correct@foomail.com' LIMIT 1);
SET @buid = (SELECT id_customer FROM customer WHERE email LIKE 'wrong@foomail.com' LIMIT 1);

UPDATE orders
SET id_customer = @uid,
id_address_delivery = @delad,
id_address_invoice = @delin
WHERE @uid is not null AND @delad is not null AND @delin is not null AND id_customer = @buid;

UPDATE cart SET
 id_customer = @uid,
 id_address_delivery = @delad,
 id_address_invoice = @delin
 WHERE @buid is not null AND @uid is not null AND @delad is not null AND @delin is not null AND id_customer = @buid;

它只是将子查询值抽象为一个变量,并将其放置在子查询所处的位置。我以前做过,不需要这种变通方法,但我认为它是在SQLServer上(也许)。
希望它能帮助别人。

相关问题