MySQL - How to select data by string length

你说的曾经没有我的故事 提交于 2019-11-26 08:47:14

问题


SELECT * FROM table ORDER BY string_length(column);

Is there a MySQL function to do this (of course instead of string_length)?


回答1:


You are looking for CHAR_LENGTH() to get the number of characters in a string.

For multi-byte charsets LENGTH() will give you the number of bytes the string occupies, while CHAR_LENGTH() will return the number of characters.




回答2:


select * from table order by length(column);

Documentation on the length() function, as well as all the other string functions, is available here.




回答3:


Having a look at MySQL documentation for the string functions, we can also use CHAR_LENGTH() and CHARACTER_LENGTH() as well.




回答4:


The function that I use to find the length of the string is length, used as follows:

SELECT * FROM table ORDER BY length(column);



回答5:


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.




回答6:


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.

select * from customer where 1 **having length(name)<2**


来源:https://stackoverflow.com/questions/1870937/mysql-how-to-select-data-by-string-length

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!