MySQL:按字段大小/长度排序


89

这是一个表结构(例如测试):

 __________________________________________
| Field Name     | Data Type               |                 
|________________|_________________________|                 
|    id          |   BIGINT (20)           |                 
|________________|_________________________|                 
|    title       |   varchar(25)           |                 
|________________|_________________________|                 
|    description |   text                  |                 
|________________|_________________________|                 

查询如下:

SELECT * FROM TEST ORDER BY description DESC;

但是我想按字段大小/字段描述的长度排序。字段类型将为TEXT或BLOB。

Answers:


167
SELECT * FROM TEST ORDER BY LENGTH(description) DESC;

LENGTH函数以字节为单位给出字符串的长度。如果要计算(多字节)字符,请改用以下CHAR_LENGTH函数:

SELECT * FROM TEST ORDER BY CHAR_LENGTH(description) DESC;

4
只需添加一个答案:如果类型是BLOB,则可以使用OCTET_LENGTH(column_name)
mastazi

根据MySQL文档,@ mastazi:OCTET_LENGTH()是LENGTH()的同义词。
Heitor

'CHAR_LENGTH'不是公认的内置函数名称。我正
遇到


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.