MYSQL: Update value from Query

2019-07-13 12:06发布

问题:

I've got a query:

SELECT a.id, b.products_id,a.zenid 
FROM titles a, ANOTHERDATABASE.products_description b
WHERE b.products_name = a.title 

It gives

id  products_id     zenid
57  3193        0
81  2037        0

What i really need is to update zendid with products_id so it becomes:

id  products_id     zenid
57  3193        3193
81  2037        2037

回答1:

This is how you are updating a table using a join in MySQL:

UPDATE titles a
  INNER JOIN ANOTHERDATABASE.products_description b
    ON b.products_name = a.title
SET a.zenid = b.products_id


回答2:

update a
set a.zenid=b.products_id
from titles a inner join ANOTHERDATABASE.products_description b 
on b.products_name = a.title