MySQL - 如何按字符串长度选择数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1870937/
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
MySQL - How to select data by string length
提问by Gal
SELECT * FROM table ORDER BY string_length(column);
Is there a MySQL function to do this (of course instead of string_length
)?
是否有 MySQL 函数来执行此操作(当然不是string_length
)?
回答by hsz
You are looking for CHAR_LENGTH()
to get the number of characters in a string.
您正在寻找CHAR_LENGTH()
获取字符串中的字符数。
For multi-byte charsets LENGTH()
will give you the number of bytes the string occupies, while CHAR_LENGTH()
will return the number of characters.
对于多字节字符集,LENGTH()
将为您提供字符串占用的字节数,而CHAR_LENGTH()
将返回字符数。
回答by Kaleb Brasee
回答by NeverHopeless
Having a look at MySQL documentationfor the string functions, we can also use CHAR_LENGTH()
and CHARACTER_LENGTH()
as well.
查看字符串函数的MySQL 文档,我们也可以使用CHAR_LENGTH()
and CHARACTER_LENGTH()
。
回答by Rito
The function that I use to find the length of the string is length
, used as follows:
我用来查找字符串长度的函数是length
,用法如下:
SELECT * FROM table ORDER BY length(column);
回答by Jesús Sánchez
I used this sentences to filter
我用这句话来过滤
SELECT table.field1, table.field2 FROM table WHERE length(field) > 10;
you can change 10 for other number that you want to filter.
您可以将 10 更改为要过滤的其他数字。
回答by Levi Maier
select * from *tablename* where 1 having length(*fieldname*)=*fieldlength*
Example if you want to select from customer the entry's with a name shorter then 2 chars.
例如,如果您想从客户中选择名称短于 2 个字符的条目。
select * from customer where 1 **having length(name)<2**