根据另一个表中的列更新表 [英] Update table based on column in another table

查看:61
本文介绍了根据另一个表中的列更新表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UPDATE bad_table,good_table
SET bad_table.post_content = good_table.post_content    
WHERE bad_table.post_type = 'post' AND 
bad_table.post_date = good_table.post_date

这是我的代码,用于根据日期值将一整列从一个表移到另一个表,我发现这在这种情况下是独一无二的.然而,我一无所获.

This is my code for getting one whole column from one table to another, based on a date value, which I found is unique for this case. However, I get nothing.

如果我选择这个,就像这样:

If I select this, like so:

SELECT * FROM bad_table,good_table
WHERE bad_table.post_type = 'post' AND 
bad_table.post_date = good_table.post_date

...我得到了我期望的所有行.我做错了什么?

...I get all the rows I would expect. What am I doing wrong?

推荐答案

假设 good_table 中可能有多个不是帖子的条目,您需要细化您的加入条件以仅选择帖子"

Assuming there could be multiple entries in good_table that are not posts, you need to refine your join condition to only select 'posts'

    update bad_table
inner join good_table on bad_table.post_date = good_table.post_date 
       and bad_table.post_type = good_table.post_type
       set bad_table.post_content = good_table.post_content
     where bad_table.post_type = 'post'

这篇关于根据另一个表中的列更新表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆