Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MYSql multiple replace query

Tags:

I have this

UPDATE  table  SET  example = REPLACE(example, '1', 'test')  WHERE example REGEXP '1$' 

So this code replaces all instances of "1" in the "example" field with "test".

I want to repeat this for 2, 3, 4 and so on.

But it would be very inefficient to use separate querys.

Is the any way I can do this with just one query?

Thanks

like image 238
jamjam Avatar asked Mar 28 '11 14:03

jamjam


People also ask

How do you replace multiple values in a column in SQL?

Using the REPLACE() function will allow you to change a single character or multiple values within a string, whether working to SELECT or UPDATE data.

How do I change all instances of word in MySQL?

MySQL REPLACE() Function The REPLACE() function replaces all occurrences of a substring within a string, with a new substring. Note: This function performs a case-sensitive replacement.

How does replace into work in MySQL?

Answer: REPLACE INTO command can be used to replace an entire row in MySQL. The row is matched against the table's PRIMARY KEY, and if the key match is successful, then the row is replaced.


1 Answers

Matryoshka-way ;-)

REPLACE(REPLACE(REPLACE(example, '3', 'test') , '2', 'test') , '1', 'test')  
like image 115
zerkms Avatar answered Sep 27 '22 02:09

zerkms