Mysql PHP Query Update

I have a table of tables and tables of two tables. These tables contain 20,000 records.

I need me to update my order table using customer_id based on email. Can I use a subquery and php OR can it be done with a single request and efficient method?

customer table

id  email     name
--------------
1 | email1 | name1
2 | email2 | name3
3 | email3 | name3

Order table

order_id  customer_id   email    product name  group_id
-------------- -----------------------------------------
1         1             email1    prod1          0
2         (NULL)        email1    prod1          1
3         1             email1    prod1          0
4         (NULL)        email2    prod1          1
5         2             email2    prod1          0
6         2             email2    prod1          1
7         (NULL)        email2    prod1          1
2         (NULL)        email1    prod1          1
  • group_id = 0 means that the client has an account
  • group_id = 1 means that the client does not have an account
+3
source share
2 answers

Perhaps this (unverified)

UPDATE orders o SET o.customer_id = (SELECT c.customer_id FROM customers c WHERE c.email = o.email)
-1
source
UPDATE customers c INNER JOIN orders o ON o.customer_id = c.id
SET o.customer_id = c.id
WHERE o.email = c.email

. INNER JOIN, , customers.

+1

Source: https://habr.com/ru/post/1774390/


All Articles