Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

sql - MySql - Way to update portion of a string?

I'm looking for a way to update just a portion of a string via MySQL query.

For example, if I have 10 records all containing 'string' as part of the field value (i.e., 'something/string', 'something/stringlookhere', 'something/string/etcetera', is there a way to change 'string' to 'anothervalue' for each row via one query, so that the result is 'something/anothervalue', 'something/anothervaluelookhere', 'something/string/etcetera', is there a way to change 'anothervalue'

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think this should work:

UPDATE table
SET field = REPLACE(field, 'string', 'anothervalue')
WHERE field LIKE '%string%';

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...