MYSQL: Update value from Query

僤鯓⒐⒋嵵緔 提交于 2019-12-24 00:46:53

问题


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  


来源:https://stackoverflow.com/questions/7145776/mysql-update-value-from-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!