mysql替换字符串计数限制

时间:2012-10-25 09:36:06

标签: mysql string csv replace

Mysql Replace函数替换指定列或字符串中找到的字符串,是否有其他方法可以限制列/字符串的替换计数?

# replace 32 from the string, there are 3 occurance
SELECT REPLACE('32,138,149,145,146,121,134,127,129,120,132,232','32','');

# note: 132,232 converted to 1 and 2
print>>> ,138,149,145,146,121,134,127,129,120,1,2

如何克服这个问题?

1 个答案:

答案 0 :(得分:2)

将干草堆与','进行虚拟连接,然后寻找针',32',并将其替换为','

SELECT REPLACE(CONCAT(',','32,138,149,145,146,121,134,127,129,120,132,232',','), ',32,', ',');

此外,如果你愿意,你也可以修剪两端的逗号。

SELECT TRIM(BOTH ',' FROM REPLACE(CONCAT(',','32,138,149,145,146,121,134,127,129,120,132,232',','), ',32,', ','));