如何在MYSQL数据库中查找和替换特定字符串中的字符串

时间:2021-08-01 18:07:47

I want to replace a particular string in mysql database, i am using this query :

我想替换mysql数据库中的特定字符串,我正在使用此查询:

UPDATE users SET name=replace(name,'raj','rajesh')

UPDATE用户SET name = replace(name,'raj','rajesh')

however what this query does is where it find raj it will replaces by rajesh for e.g if there is a string raju in databse after running this query raju becomes rajeshu which i dont want. i want a query which matches the replace string exactly means after running a query only 'raj' should get replaced with 'rajesh' and 'raju' should remain as is.. can someone please help??

然而,这个查询所做的是它找到raj它将替换为rajesh,例如,如果在运行此查询后数据库中存在字符串raju raju成为我不想要的rajeshu。我想要一个匹配替换字符串的查询正好意味着在运行查询后只有'raj'应该替换为'rajesh'并且'raju'应该保持原样..有人可以帮助吗?

4 个解决方案

#1


2  

Try below query to replace raj with rajesh

尝试以下查询用rajesh替换raj

update users set name=replace(name,' raj ',' rajesh ');

OR

 update users set name=replace(name,'raj ','rajesh ') where name like '% raj %';

#2


2  

Try this it will definitely work for you.

试试这个肯定会对你有用。

update users 
set name=replace(LOWER(name),'raj','rajesh') 
where 
name like 'raj %' 
OR 
name like '% raj %'
OR
name = 'raj'

#3


2  

This query works for me:

此查询适用于我:

UPDATE users 
SET name = replace(name,'raj','rajesh')
WHERE name = 'raj'

#4


0  

Try this:

UPDATE users 
SET name = 'rajesh' 
WHERE name = 'raj';

#1


2  

Try below query to replace raj with rajesh

尝试以下查询用rajesh替换raj

update users set name=replace(name,' raj ',' rajesh ');

OR

 update users set name=replace(name,'raj ','rajesh ') where name like '% raj %';

#2


2  

Try this it will definitely work for you.

试试这个肯定会对你有用。

update users 
set name=replace(LOWER(name),'raj','rajesh') 
where 
name like 'raj %' 
OR 
name like '% raj %'
OR
name = 'raj'

#3


2  

This query works for me:

此查询适用于我:

UPDATE users 
SET name = replace(name,'raj','rajesh')
WHERE name = 'raj'

#4


0  

Try this:

UPDATE users 
SET name = 'rajesh' 
WHERE name = 'raj';