[英]Replace a string in MySQL in a specific field depending on the value of another field
如何根据另一个字段的值在MySQL中查找和替换? 例如,
我希望将“ post_parent”列中的所有“ 0”值替换为“ 126810” ...……但前提是这些条目在“ post_type”字段中也具有“ topic”的值。
我以为会是这样,但是似乎不起作用,语法问题以及所有这些:
update wp_posts set post_parent = replace(post_parent,`0`,`126810`)
WHERE `post_type` LIKE 'topic');
我还猜想,即使我可以使用它,我也会用1126810、2126810等替换所有post_parent值10、20、30等。
有人偶然知道我该如何解决这两个问题?
您应该能够扩展WHERE
子句:
update wp_posts
set post_parent = replace(post_parent,0,126810)
WHERE post_type = 'topic'
AND post_parent = 0;
也许什么case when
会帮助您。
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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.