用where条件替换sql

时间:2015-09-25 15:30:35

标签: mysql sql database

我知道,我可以通过

找到我需要的信息
SELECT * from x6fnckq9h_posts
where post_status = 'publish' and post_content like '%new.%'

另外,我知道我可以用

替换字段中的文本
update TABLE_NAME 
set FIELD_NAME = 
       replace(FIELD_NAME, 'find this string', 'replace found string with this string');

我需要的是:用'%new.%'

替换每个''

那么,发送

是否正确
update x6fnckq9h_posts
  set post_content = replace(post_content, 'new.', '') 
WHERE post_status = 'publish' and post_content like '%new.%';

2 个答案:

答案 0 :(得分:1)

如果您感到紧张,请将更新转换为选择并注视输出以查看其是否正常:

select
  post_content old_post_content,
  replace(post_content, 'new.', '') new_post_content
from x6fnckq9h_posts
where post_status = 'publish'
and post_content like '%new.%'

如果您喜欢建议的更改,并且只要您使用完全相同的where子句,运行更新就可以了。

答案 1 :(得分:0)

只需对两个字段进行选择,而不是运行更新。

SELECT post_content, replace(post_content, 'new.', '') 
FROM x6fnckq9h_posts
WHERE post_status = 'publish' 
  AND post_content like '%new.%';