我正在尝试更新website_id
等于6的客户,并且当website_id
为3时,他们的电子邮件不会重复。
我已经能够让website_id
为6的所有客户以及website_id
为3时他们的电子邮件不重复,SQL语句如下所示。
SELECT
*
FROM customer_entity
WHERE website_id = 6
AND email NOT IN (SELECT
email
FROM customer_entity
WHERE website_id = 3);
现在,当我尝试更新所有site_id和store_id等于3到6的客户时,他们的电子邮件在store_id = 3中不重复
UPDATE customer_entity customers
SET customers.website_id = 3, customers.store_id = 3
WHERE customers.website_id = 6 AND customers.email
NOT IN (SELECT email FROM customer_entity WHERE website_id = 3);
我收到以下错误
您无法在FROM子句
中为更新指定目标表'customers'
我如何实现我想要做的事情?
编辑: 我也试过没有别名,我仍然得到同样的错误。
答案 0 :(得分:0)
从代码中删除别名。 试试这个:
UPDATE customer_entity
SET website_id = 3, store_id = 3
WHERE website_id = 6 AND email
NOT IN (SELECT email FROM customer_entity WHERE website_id = 3);
答案 1 :(得分:0)
您必须使用MySQL。您可以使用join
执行此操作。我会把它写成:
update customer_entity ce join
(SELECT email
FROM customer_entity
GROUP BY email
HAVING SUM(website_id = 6) > 0 AND
SUM(website_id = 3) = 0
) e
on ce.email = e.email
set ce.website_id = 3,
ce.store_id = 3 ;
答案 2 :(得分:0)
这是为了更正或增强您的更新声明
更新FROM而不是EXISTS否则基本更新而不是
UPDATE customers
SET website_id = 3
,store_id = 3
from customer_entity customers
WHERE website_id = 6
AND NOT EXISTS
(SELECT 1
FROM customer_entity c
WHERE c.website_id = 3
c.email = customers.email
);
更新而不是
UPDATE customer_entity
SET website_id = 3
,store_id = 3
WHERE website_id = 6
AND email not in
(SELECT email
FROM customer_entity
WHERE website_id = 3
);
然而从你的问题看这个陈述
现在,当我尝试更新所有拥有website_id和的客户时 store_id等于3到6,其中的电子邮件不重复 store_id = 3
这应该是你的解决方案: 更新和不存在
UPDATE customers
SET website_id = 6 -- Now when I try to update all customers that have website_id and store_id equal to 3 to 6
,store_id = 6
from customer_entity customers
WHERE website_id = 3 and store_id = 3 -- Now when I try to update all customers that have website_id and store_id equal to 3 to 6
AND NOT EXISTS
(SELECT 1
FROM customer_entity c
WHERE c.store_id = customers.store_id -- where their email is not duplicate in store_id = 3
and c.email = customers.email
);
更新而不是
UPDATE customer_entity
SET website_id = 6 -- Now when I try to update all customers that have website_id and store_id equal to 3 to 6
,store_id = 6
WHERE website_id = 3 and store_id = 3 -- Now when I try to update all customers that have website_id and store_id equal to 3 to 6
AND email NOT IN
(SELECT email
FROM customer_entity c
WHERE store_id = 3 -- where their email is not duplicate in store_id = 3
);
答案 3 :(得分:0)
以下是我自己的问题的答案。这里的许多答案都使用JOINS到一个表,所以对于MYSQL,这不起作用,因为你无法在同一个SELECT JOIN上更新。
我的解决方案是创建一个临时表并存储所有具有store_id和website_id = 6的值以及WHERE电子邮件不重复WHERE store_id和website_id = 3.
CREATE TEMPORARY TABLE customer_temp_table(
SELECT email FROM customer_entity
WHERE
website_id = 6
AND
email NOT IN (SELECT email FROM customer_entity WHERE website_id = 3));
然后我使用临时表的结果更新客户实体表。
UPDATE customer_entity AS customers SET customers.website_id = 3, customers.store_id = 3
WHERE customers.email IN (SELECT email FROM customer_temp_table) AND customers.website_id = 6;
答案 4 :(得分:-1)
如果您在表格上执行 UPDATE / INSERT / DELETE ,则无法在内部查询中引用该表格(但您可以从该外部查询中引用该字段表......)
解决方案是用(SELECT * FROM customer_entity)替换子查询中的myTable实例,就像这样
SELECT * FROM customer_entity WHERE website_id = 3 as something
您可以在此处找到更多信息:You can't specify target table for update in FROM clause