I'm newbie in SQL questions, and today i have a big problem.
I need change many fields in my SQL, about 4,5k. But have some rules to the query change, to don't change wrong fields where don't need changed.
In table wp_postmeta i have four columns:
meta_id | post_id | meta_key | meta_value
I need change the value of meta_value
, BUT ONLY if the current value is 'Y' or 'Empty' and REPLACE to value 'X', AND ONLY in lines where meta_key
value is old_price
How i can do it?
I'm newbie in SQL questions, and today i have a big problem.
I need change many fields in my SQL, about 4,5k. But have some rules to the query change, to don't change wrong fields where don't need changed.
In table wp_postmeta i have four columns:
meta_id | post_id | meta_key | meta_value
I need change the value of meta_value
, BUT ONLY if the current value is 'Y' or 'Empty' and REPLACE to value 'X', AND ONLY in lines where meta_key
value is old_price
How i can do it?
Share Improve this question asked Jan 1, 2020 at 22:05 Felipe GarciaFelipe Garcia 31 bronze badge1 Answer
Reset to default 0Recommend a database backup before you do this.
** updated the statement to specify "Y", empty string, or null **
global $wpdb;
$wpdb->query( "update {$wpdb->postmeta} SET meta_value = 'X' WHERE meta_key = 'old_price' AND ( meta_value = 'Y' OR meta_value = '' OR meta_value IS NULL ) );" );