1

我可能做错了什么,我之前搜索过它,我发现一些解决方法告诉我它在 MySQL 上不可能,其他人发布它是由于 MySQL 优化器,所以你可以简单地关闭它并继续,但是这对我不起作用。

我只想要一个简单的解决方法来解决这个问题。我可以找到 id_address_delivery 和 id_address_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 工作台上的 FROM 子句中指定目标表“订单”进行更新

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');

/编辑:

我阅读了一些被标记为相同问题的帖子,但在那里我找到了其他条款的一些解决方法,我无法弄清楚如何在我的特定情况下应用相同的方法。

我该如何进行?谢谢。

4

3 回答 3

0

我终于处理了它设置变量,仍然不知道为什么这会导致超时。如果某些 var init 失败,我在 where 子句上添加了 null 保护以不插入 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;

它只是将子查询值抽象为变量并将其放在子查询所在的位置。我以前做过,不需要这种解决方法,但我认为它是在 sql server 上(也许)。

希望它可以帮助某人。

于 2018-09-10T11:23:04.430 回答
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
于 2018-09-10T11:55:27.750 回答
0

作为其他答案,如果您在表上执行 UPDATE/INSERT/DELETE,则不能在内部查询中引用该表(但是您可以从该外部表中引用一个字段......)

检查以下与您的问题相同的问题,它们已解决。

MySQL 错误 1093 - 无法在 FROM 子句中指定要更新的目标表

您不能在 FROM 子句中指定要更新的目标表

于 2018-09-10T11:03:25.027 回答