views:

1180

answers:

1

In MySQL, how can I order my query by character count?

+6  A: 

Try using the LENGTH function:

SELECT * FROM table ORDER BY LENGTH(myField);

Depending on what you're doing, you might want to use CHAR_LENGTH instead:

A multi-byte character counts as a single character. This means that for a string containing five two-byte characters, LENGTH() returns 10, whereas CHAR_LENGTH() returns 5.

If you don't know what that means, you probably want LENGTH.

Paolo Bergantino
What does ... do in MySQL? ;-)
Dems
Yeah, I edited it to just use the dreaded asterisk. :) I guess I'd rather put up an inefficient query than a non-working one.
Paolo Bergantino
meh, even MySQL's own documentation uses ellipsis to signify omitted code: http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html . It's quite common to use "..." to represent variable parameters or omissions in programming.
Calvin
Well they use it on inline text, but whenever they separate out into a code block they use table names. I feel that's probably the best way to go.
Paolo Bergantino
Wow, I could never have predicted that I would cause such a principled exchange, I was only having a laugh :)
Dems