简体   繁体   English

从另一个表更新多个MySQL行

[英]Update multiple MySQL rows from another table

Ok I have two tables: articles and posts. 好的,我有两个表格:文章和帖子。

In articles, I have the "image" column where each entry has a name for the image. 在文章中,我有“图像”列,其中每个条目都有图像的名称。

In the posts table, I have the "post_image" column where I want to put all the values from the articles table, with the matching article id. 在posts表中,我有“ post_image”列,我想在其中放置articles表中的所有值以及匹配的article id。

This is what I'm trying to do but of course it spits out an error since it's more than one value. 这是我要尝试的操作,但是由于它不止一个值,因此它会吐出一个错误。 I know it should be a JOIN but I get lost with joins. 我知道这应该是一个JOIN,但是我迷失了join。 Any help? 有什么帮助吗?

UPDATE posts SET post_image = (
SELECT image FROM articles, posts WHERE article_id = ID
)

I tried this: 我尝试了这个:

UPDATE posts SET post_image = (SELECT image FROM articles WHERE article_id = ID LIMIT 1)

And all it does is update all the entries of "post_image" with the first value of "articles". 它所做的就是用“ articles”的第一个值更新“ post_image”的所有条目。

Actually, I was doing the wrong thing, it was much simpler than that. 实际上,我做错了事情,这比那简单得多。 Here's what finally worked for me: 这终于对我有用:

UPDATE posts, articles
SET posts.post_image = articles.image
WHERE posts.ID = articles.article_id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM