SQL / MySQL - 按列长度排序 [英] SQL / MySQL - Order By Length of Column
问题描述
在MySQL中,有没有办法按照列的长度(字符)来排序结果?
例如:
myColumn
________________
lor
lorem
lorem ip
lorem ips
lorem ipsum
我想按照最小列长度lor最大列长度,lorem ipsum。
您可以使用 CHAR_LENGTH()
功能:
ORDER BY CHAR_LENGTH(column)
注意 CHAR_LENGTH c $ c>也适用于Unicode字符串,其中
LENGTH()
适用于拉丁文,但可能会给出意想不到的Unicode结果:
CHAR_LENGTH(str)
返回字符串str的长度,以字符为单位。多字节字符计为单个字符。这意味着对于包含五个双字节字符的字符串,LENGTH()返回10,而CHAR_LENGTH()返回5.
In MySQL, is there a way to order my results by the length (characters) of a column?
For example:
myColumn
________________
lor
lorem
lorem ip
lorem ips
lorem ipsum
I would like to order my results by the smallest column length first, "lor", and ending in the largest column length, "lorem ipsum". This should include column lengths of 0 too.
Any suggestions gratefully received...
You can use CHAR_LENGTH()
function:
ORDER BY CHAR_LENGTH( column )
Notice that CHAR_LENGTH()
works for Unicode strings as well, where LENGTH()
is OK for Latin but may give unexpected results with Unicode:
CHAR_LENGTH(str)
Returns the length of the string str, measured in characters. A multi-byte character counts as a single character. This means that for a string containing five two-byte characters, LENGTH() returns 10, whereas CHAR_LENGTH() returns 5.
这篇关于SQL / MySQL - 按列长度排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!