我想从MySQL中的字符串中删除前导和尾随空格(SP,\ n,\ t,最终\ r \ n)。数据已经存在于MySQL表中,我无法检索它们以便在PHP中进行处理,因为这应该太慢了。
我试过这种语法:
UPDATE table set field = TRIM(BOTH '\t' FROM TRIM(BOTH '\n' FROM TRIM(field)));
但是,这种方式会删除spaces, then
\ n , then
\ t in this order, and I want to remove all spaces disregarding their order (ie:
“\ n \ t \ t \ n \ n \ t \ hello \ t \ n \ n \ n” would return only
“你好”`。
我想我需要创建一个函数(CREATE FUNCTION MY_TRIM
...),但在做这样的工作之前,我想知道是否有更简单的方法。
答案 0 :(得分:0)
为什么不简单地使用替换
例如
update table set field = replace(replace(replace(field,"\t",""),"\n","")," ","")
答案 1 :(得分:0)
你可以试试这个:
UPDATE table SET `field`=TRIM(BOTH '\t' OR '\n' FROM TRIM(BOTH '\n' OR '\t' FROM TRIM(field)));