Adeerlike
Adeerlike

Reputation: 467

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?

Upvotes: 0

Views: 2623

Answers (1)

Jeff Paquette
Jeff Paquette

Reputation: 7127

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'

Upvotes: 2

Related Questions