1

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

如何克服这一点?

4

1 回答 1

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,', ','));
于 2012-10-25T10:02:03.587 回答