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

时间:2010-04-03 17:50:48

标签: sql mysql sql-order-by

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

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

如下的查询:

SELECT * FROM TEST ORDER BY description DESC;

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

3 个答案:

答案 0 :(得分:131)

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

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

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

答案 1 :(得分:4)

SELECT * FROM TEST ORDER BY CHAR_LENGTH(description);

答案 2 :(得分:3)

对于那些使用MS SQL的人

SELECT * FROM TEST ORDER BY LEN(field)