MySQL 通过 strlen 从数据库中选择行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5048457/
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 18:50:22  来源:igfitidea点击:

Select rows from database by strlen

mysqlstringstring-length

提问by santa

Is there a way to select database table rows where a given value is a certain length, for example, less than 5 chars long?

有没有办法选择给定值具有特定长度(例如,长度小于 5 个字符)的数据库表行?

In PHP that would be strlen.

在 PHP 中,这将是strlen.

Is there anything similar in MySQL?

MySQL中有没有类似的东西?

回答by Derek Prior

SELECT * FROM table_name WHERE CHAR_LENGTH(column_name) < 5

回答by Timo Huovinen

LENGTH("my_string")Return the length of a string in bytes

LENGTH("my_string")以字节为单位返回字符串的长度

SELECT * FROM table_name WHERE LENGTH(column_name) < 5

Keep in mind that characters can be made up of multiple bytes like those in UTF-8.

请记住,字符可以由多个字节组成,如 UTF-8 中的字节。