简体   繁体   中英

Update table based on column in another table

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?

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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