更新不在 Select 中的列

Update Where Column not in Select

我正在尝试更新 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);

现在,当我尝试更新所有 website_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);

我收到以下错误

You can't specify target table 'customers' for update in FROM clause

我怎样才能实现我想要做的事情?

编辑: 我也尝试过不使用别名,但仍然出现相同的错误。

从代码中删除别名。 试试这个:

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

您必须使用 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 ;

如果您在 table 上执行 UPDATE/INSERT/DELETE,则无法在内部查询中引用 table(但是,您可以从外部 table...)

引用一个字段

解决方法是将子查询中myTable的实例替换为(SELECT * FROM customer_entity),像这样

SELECT * FROM customer_entity WHERE website_id = 3 as something

您可以在此处找到更多信息:You can't specify target table for update in FROM clause

这是为了更正或增强您的更新声明

Update FROM and NOT EXISTS otherwise the basic UPDATE and NOT IN

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

然而从你的问题来看这个陈述

Now when I try to update all customers that have website_id and store_id equal to 3 to 6 where their email is not duplicate in 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 

                );

这是我自己的问题的答案。这里的许多答案都将 JOINS 用于一个 table,因此对于 MYSQL,这是行不通的,因为您无法在同一个 SELECT JOIN.

上进行更新

我的解决方案是创建一个临时 table 并存储所有具有 store_id 和 website_id = 6 的值,并且电子邮件不重复 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));

然后我使用来自临时 table 的结果更新客户实体 table。

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;