开发者

MySQL: Order by field size/length

开发者 https://www.devze.com 2022-12-25 19:22 出处:网络
Here is a table structure (e.g. test): Field Name Data Type id BIGINT (20) title varchar(25) Description Text

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 desc开发者_StackOverflow社区ription DESC;

But I would like to order by the field size/length of the field description.

The field type will be TEXT or BLOB.


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;


For those using MS SQL

SELECT * FROM TEST ORDER BY LEN(field)


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号