MySQL: Order by field size/length

Sadi picture Sadi · Apr 3, 2010 · Viewed 70.2k times · Source

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

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.

Answer

João Silva picture João Silva · Apr 3, 2010
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;