I have a table with a column that has a string that is listed in a variety of comma-delimited ways. I need to remove the string or replace the string - is it possible to use REPLACE on the column twice or more in the same mysql query?
myTable
id myString
================
1 15
2 15,30
3 30,45
4 30
5 30,15,45
6 45,15
I need to replace or remove all instances of 15
, but I need to account for 15
being the only value OR one of several values that are separated by a comma. IF one of several values, then I need to account removing extra commas that would be left by the first replace.
Just replacing the 15 would leaving trailing commas behind:
IE: $query = "UPDATE myTable SET myString=(REPLACE(myString,'15',''))" ;
myTable
id myString
================
1
2 ,30
3 30,45
4 30
5 30,,45
6 45,
The replace needs to account for the comma before (or after) the value - if it exists and as well as replace any leading commas.
$query = "UPDATE myTable SET myString=(REPLACE(myString,'15','')), // replace string
myString=(REPLACE(myString, ',,' , ',')), //then remove double commas
myString=(REPLACE(myString, ',%' , '')), //then leading commas
myString=(REPLACE(myString, '%,' , ''))" ; //then trailing commas
Would the above work...replacing the matching with the first replace and the 2nd, 3rd, 4th replace clean up trailing commas? Or will it not work because each replace is only evaluating the original content, and not the content after it was already updated?
What is a better method to do this?