GrutBrushes
GrutBrushes

Reputation: 35

How can I update all the records that match my SELECT query in SQL?

I need to change the post_status field for all the records from February 2017 in a particular table to wc-pending

I have figured out how to select all those records:

SELECT * FROM `wp_posts` WHERE `post_title` LIKE '%FEB%2017%'

And I think this will change the field as I need to:

SET post_status='wc-pending'

But I am stumped on how to put these two together to update the post_status fields for only those records that match the SELECT query.

My hunch is that the following might do it if I execute it while in that table

SET post_status='wc-pending' WHERE `post_title` LIKE '%FEB%2017%'

Does that seem correct? Any guidance is appreciated. I am totally new to SQL. Thanks!

Upvotes: 0

Views: 20

Answers (1)

yBrodsky
yBrodsky

Reputation: 5041

Your hunch is correct.

UPDATE wp_posts SET post_status='wc-pending' WHERE `post_title` LIKE '%FEB%2017%'

You can even do more complex stuff, like add some inner joins. By the way, there's a way in phpmyadmin to simulate the query, see how it turns out.

Upvotes: 1

Related Questions