문제

How can I find and replace in MySQL depending on the values of another field? For example,

I want to replace all of the "0" values in the column "post_parent" with "126810"...but only if those entries also have a value of "topic" in the "post_type" field.

I thought it would be something like this but it doesn't seem to work, syntax issues and all:

update wp_posts set post_parent = replace(post_parent,`0`,`126810`) 
WHERE `post_type` LIKE 'topic');

I also guess that even if I could have gotten that to work, I would have replaced all post_parent values of 10, 20, 30, etc. with 1126810, 2126810, etc.

Anyone happen to know how I could solve both of those issues?

도움이 되었습니까?

해결책

You should be able to extend your WHERE clause:

update wp_posts 
set post_parent = replace(post_parent,0,126810)
WHERE post_type = 'topic'
  AND post_parent = 0;

See SQL Fiddle with Demo

다른 팁

Perhaps a case when would help you.

update wp_posts 
set post_parent = 
case when (`post_type` LIKE 'topic'
and post_parent = `0`)
then
replace(post_parent,`0`,`126810`)
end

;

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top