SQL / MySQL - Order By Length of Column

前端 未结 3 637
南旧
南旧 2021-01-15 17:45

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          


        
相关标签:
3条回答
  • 2021-01-15 18:29

    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.

    0 讨论(0)
  • 2021-01-15 18:29

    Have a look at the LENGTH() operator.

    ORDER BY LENGTH(myColumn)
    
    0 讨论(0)
  • 2021-01-15 18:39
    SELECT * FROM my_table ORDER BY LENGTH(my_column)
    
    0 讨论(0)
提交回复
热议问题