mySql只替换列中的单个单词

时间:2014-01-22 13:31:37

标签: mysql

单独使用mySql,我试图用microsoft-corp替换microsoft这个词。

我尝试使用这个sql update names set names = replace(names,'microsoft','microsoft-corp'),但它取代了每个microsoft。有没有办法让它只替换微软这个词?

原始数据

"id"    "names"
"1"     "/microsoft"
"2"     "/microsoft/microsoft-word"
"3"     "/microsoft/microsoft-word/microsoft-word-tutorials"
"4"     "/microsoft/microsoft-word/microsoft-word-tutorials/examples"

"id"    "names"
"1"     "microsoft >"
"2"     "microsoft > microsoft-word"
"3"     "microsoft > microsoft-word > microsoft-word-tutorials"
"4"     "microsoft > microsoft-word > microsoft-word-tutorials > examples"

预期的正确结果

"id"    "names"
"1"     "/microsoft-corp"
"2"     "/microsoft-corp/microsoft-word"
"3"     "/microsoft-corp/microsoft-word/microsoft-word-tutorials"
"4"     "/microsoft-corp/microsoft-word/microsoft-word-tutorials/examples"

使用上面的查询

显示当前不正确的结果
"id"    "names"
"1"     "/microsoft-corp"
"2"     "/microsoft-corp/microsoft-corp-word"
"3"     "/microsoft-corp/microsoft-corp-word/microsoft-corp-word-tutorials"
"4"     "/microsoft-corp/microsoft-corp-word/microsoft-corp-word-tutorials/examples"

2 个答案:

答案 0 :(得分:2)

怎么样:

update names set names = replace(names,'/microsoft/','/microsoft-corp/')

update names set names = '/microsoft-corp' where names = '/microsoft'

update names set names = replace(names,'microsoft >','microsoft-corp >')

答案 1 :(得分:0)

我可能会在两次传球中做到这一点,尽管你可以一次传球。

update names set names = replace(names,'/microsoft/','/microsoft-corp/');
update names set names = '/microsoft-corp' where names = '/microsoft';