MySQL:按字段大小/长度排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2572118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQL: Order by field size/length
提问by Sadi
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/lengthof the field description. The field type will be TEXT or BLOB.
但我想按字段描述的字段大小/长度排序。字段类型将为 TEXT 或 BLOB。
回答by Jo?o Silva
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:
该LENGTH
函数以字节为单位给出字符串的长度。如果要计算(多字节)字符,请改用该CHAR_LENGTH
函数:
SELECT * FROM TEST ORDER BY CHAR_LENGTH(description) DESC;
回答by Mike Sherov
SELECT * FROM TEST ORDER BY CHAR_LENGTH(description);
回答by mfrederick
For those using MS SQL
对于那些使用 MS SQL 的人
SELECT * FROM TEST ORDER BY LEN(field)