MYSQL 如何在选择查询中使用修剪

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3632760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 17:02:59  来源:igfitidea点击:

MYSQL How to use trim in select query

sqlmysqlstring

提问by Bharanikumar

my table have set of records around 50,

我的桌子上有大约 50 条记录,

in the table i have column called USERNAME, but some of username leading and trailing have the white space ,

在表中,我有一列名为USERNAME,但一些用户名前导和尾随有空格,

so am not getting exact order result because of white space,

因为空白,所以没有得到确切的订单结果,

So tell me how to use the trim in SELECTquery ,

所以告诉我如何在SELECT查询中使用修剪,

Thanks

谢谢

回答by OMG Ponies

You can use TRIMin the ORDER BY:

您可以在 ORDER BY 中使用TRIM

ORDER BY TRIM(username)

...but this will only trim excess space on the left and right side of the text, not in between.

...但这只会修剪文本左侧和右侧的多余空间,而不是介于两者之间。

Using TRIM in the SELECT is as easy as:

在 SELECT 中使用 TRIM 非常简单:

SELECT TRIM(username) AS username
  FROM your_table

回答by slon

You can use REPLACEin your query to remove all whitespaces from strings, e.g.:

您可以在查询中使用REPLACE从字符串中删除所有空格,例如:

SELECT REPLACE(A.Postal, ' ', '') AS Postal FROM ADDRESS

Be aware that this will remove all whitespaces, not only the leading and tailing ones.

请注意,这将删除所有空格,而不仅仅是开头和结尾的空格。