-2

I am trying to update customers where their website_id is equal 6 and their email is not duplicate when website_id is 3.

I have been able to get all customer where website_id is 6 and their email is not duplicate where website_id is 3, the SQL statement bellow does that.

SELECT
  *
FROM customer_entity
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 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);

I get the following error

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

How can I achieve what I am trying to do?

EDIT: I have also tried without the alias and I still get the same error.

André Ferraz
  • 1,511
  • 11
  • 29

5 Answers5

0

Remove the aliases from the code. Try this:

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);
C B
  • 1,677
  • 6
  • 18
  • 20
0

You must be using MySQL. You can do this with a join. I would write this as:

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 ;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0

This is to correct or enhance your update statement

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 and NOT IN

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

however looking at this statement from your problem

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

this should be your solution : UPDATE and NOT EXISTS

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 and NOT IN

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 

                );
RoMEoMusTDiE
  • 4,739
  • 1
  • 17
  • 26
0

Here is the answer for my own question. Many of the answer here use JOINS to one table so for MYSQL, this does not work because you can't update on the same SELECT JOIN.

My solution was to create a temporary table and store all values which had store_id and website_id = 6 and WHERE the email was not duplicate WHERE store_id and 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));

Then I update the the customer entity table using results from the temp 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;
André Ferraz
  • 1,511
  • 11
  • 29
-1

If you're doing an UPDATE/INSERT/DELETE on a table, you can't reference that table in an inner query (you can however reference a field from that outer table...)

The solution is to replace the instance of myTable in the sub-query with (SELECT * FROM customer_entity), like this

SELECT * FROM customer_entity WHERE website_id = 3 as something

You can find more info here : You can't specify target table for update in FROM clause

Community
  • 1
  • 1
Mojo Allmighty
  • 793
  • 7
  • 18