我可能做错了事,我之前进行了搜索,发现一些变通办法告诉我在MySQL上无法实现,其他人则认为这是由于MySQL优化器造成的,因此您可以将其关闭并继续,但这对我不起作用。
我只想要一个简单的解决方法来解决这个问题。我唯一可以在订单表中找到id_address_delivery和id_address_invoice的表,该数据也位于其他表上,但是可以为null,因此无法选择在其他位置进行搜索。
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工作台上的FROM子句中指定要更新的目标表'orders'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秒并获得相同的超时消息。
编辑:
我尝试使用别名,但没有结果。同样的错误:
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');
/ EDIT:
我阅读了一些被标记为同一问题的帖子,但是在其他子句中找到了一些解决方法,并且我不知道如何在我的特殊情况下应用相同的内容。
我该如何进行? 谢谢。
答案 0 :(得分:0)
作为其他答案,如果您要对表进行UPDATE / INSERT / DELETE,则无法在内部查询中引用该表(但是,您可以引用该外部表中的字段...)>
检查以下与您的问题相同的问题,即可解决。
MySQL Error 1093 - Can't specify target table for update in FROM clause
答案 1 :(得分:0)
我终于处理了设置vars的问题,但仍然不知道为什么这会导致超时。 我在where子句上添加了空保护,以便在某些var初始化失败时不插入空值。
为您的利益提供了我的工作示例副本:
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;
这只是将子查询值抽象为变量,并将其放在子查询所在的位置。 我在不需要此解决方法之前就已经做到了,但是我认为它在sql服务器上(也许)。
希望它可以帮助某人。
答案 2 :(得分:0)
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