Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何执行MYSQL REPLACE以|(管道)替换多个或单个空间_Mysql_C_Regex - Fatal编程技术网

如何执行MYSQL REPLACE以|(管道)替换多个或单个空间

如何执行MYSQL REPLACE以|(管道)替换多个或单个空间,mysql,c,regex,Mysql,C,Regex,只适用于一个空间 SELECT * FROM `spamfilter` WHERE `words` REGEXP (REPLACE('test won hello spam', " ", "|")) 适用于两个空间 SELECT * FROM `spamfilter` WHERE `words` REGEXP (REPLACE('test[2 spaces]won[2 spaces]hello[2 spaces]spam', "[2 spaces]", "|")) 无法在一个查询中使用多个或

只适用于一个空间

SELECT * FROM `spamfilter` WHERE `words` REGEXP (REPLACE('test won hello spam', " ", "|"))
适用于两个空间

SELECT * FROM `spamfilter` WHERE `words` REGEXP (REPLACE('test[2 spaces]won[2 spaces]hello[2 spaces]spam', "[2 spaces]", "|"))
无法在一个查询中使用多个或单个空格


这只适用于一个空格,如果在一个查询中有多个空格,我需要它工作。

我假设您知道文本中多个空格的最大长度

假设最大长度为3:在这种情况下,您必须像这样运行查询3次:

SELECT * FROM `spamfilter` WHERE `words` REGEXP (REPLACE('test[5 space]won[2 space]hello[1 space]spam', " ", "|"))

可能有更好的方法,但这个方法也应该有效。

我认为这个方法最多可以处理12个空格:

// for three spaces
 update tableName set colName = REPLACE(LTRIM(RTRIM(colName)), '   ', '|') where colName like '% %'

//for two spaces
 update tableName set colName = REPLACE(LTRIM(RTRIM(colName)), '  ', '|') where colName like '% %'

//for one space
update tableName set colName = REPLACE(LTRIM(RTRIM(colName)), ' ', ' ') where colName like '% %'
REPLACE(
REPLACE(
REPLACE(
REPLACE(TRIM(col), '     ', ' ')  --5
                   '   ', ' ')    --3
                   '  ', ' ')     --2
                   ' ', '|')