MySQL: Order by field size/length

我是研究僧i 提交于 2019-11-27 00:49:07

问题


Here is a table structure (e.g. test):

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

A query like:

SELECT * FROM TEST ORDER BY description DESC;

But I would like to order by the field size/length of the field description. The field type will be TEXT or BLOB.


回答1:


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

The LENGTH function gives the length of string in bytes. If you want to count (multi-byte) characters, use the CHAR_LENGTH function instead:

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



回答2:


SELECT * FROM TEST ORDER BY CHAR_LENGTH(description);



回答3:


For those using MS SQL

SELECT * FROM TEST ORDER BY LEN(field)


来源:https://stackoverflow.com/questions/2572118/mysql-order-by-field-size-length

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