php SQL 查询:按字符长度排序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3606907/
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
SQL Query: order by length of characters?
提问by TorbenL
Morning, is it possible to order sql data rows by the length of characters?
早上好,是否可以按字符长度对sql数据行进行排序?
e.g. SELECT * FROM database ORDER BY data.length()
例如 SELECT * FROM database ORDER BY data.length()
回答by tamasd
I think you want to use this: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_char-length
我想你想用这个:http: //dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_char-length
SELECT * FROM table ORDER BY CHAR_LENGTH(field)
You can use just simply LENGTH(), but beware, because it counts the byte number (which won't give you the expected result with multibyte strings).
您可以简单地使用 LENGTH(),但要注意,因为它计算字节数(这不会为您提供多字节字符串的预期结果)。
回答by Alex Reitbort
SELECT * FROM database ORDER BY Len(data)
回答by Michael Pakhantsov
SELECT * FROM table ORDER BY length(data) desc
Where data is varchar field
其中 data 是 varchar 字段
回答by Kashif
SELECT * FROM YourTable ORDER BY LENGTH(Column_Name) DESC
e.g;
例如;
SELECT * FROM Customer ORDER BY LENGTH(CustomerName) DESC
回答by zeeawan
For anyone doing with Sqlite
对于任何使用Sqlite 的人
SELECT * FROM table ORDER BY LENGTH(field) DESC