简体   繁体   English

如何删除mysql查询中的尾随或前导字符?

[英]how to remove trailing or leading charaters in a mysql query?

I have a Japanese dictionary table, the English words that correspond to the Japanese look something like this: 我有一个日语字典表,对应于日语的英语单词看起来像这样:

jap       eng                   marks     id
テクスト    /  text/ text book/     (n)     31112

I want to remove the leading and trailing slashes and spaces. 我想删除前导和尾部斜杠和空格。 it almost always have leading and trailing slashes but also have valid slashes separating the multiple English meanings. 它几乎总是有前导和斜杠,但也有有效的斜杠分隔多个英文含义。 How can i write a query that will remove a given string if it is leading or trailing? 我如何编写一个查询,如果它是前导或尾随,将删除给定的字符串? In this case the "/ " is leading and "/" is trailing but sometimes there is multiple slashes on the end or front and sometimes with or without unnecessary spaces 在这种情况下,“/”是前导,“/”是尾随,但有时在末端或前面有多个斜线,有时有或没有不必要的空格

so im thinking if I had some query like 所以我想如果我有一些查询

set eng = REPLACE_LEADING_OR_TRAILING(" /", "", eng)

i could just query it with all the possibilities 我可以用所有可能性来查询它

REPLACE_LEADING_OR_TRAILING("//", "", eng)
REPLACE_LEADING_OR_TRAILING(" /", "", eng)
REPLACE_LEADING_OR_TRAILING(" / ", "", eng)
REPLACE_LEADING_OR_TRAILING("/ ", "", eng)
REPLACE_LEADING_OR_TRAILING("/", "", eng)

i know about the REPLACE() function but i can't see how i would do it with that because of the valid slashes in between multiple English meanings. 我知道REPLACE()函数,但由于多个英语含义之间的有效斜杠,我无法看到我将如何处理它。

You want to use the TRIM function: 您想使用TRIM功能:

To remove spaces, just call it with no other parameter than your field/variable 要删除空格,只需使用除字段/变量之外的其他参数调用它

SELECT TRIM(eng)

To remove another character, use this version instead: 要删除其他角色,请改用此版本:

SELECT TRIM(BOTH '/' FROM eng);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM