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
2009-04-19 14:54:28
What does ... do in MySQL? ;-)
Dems
2009-04-19 14:56:52
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
2009-04-19 14:57:35
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
2009-04-19 15:28:56
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
2009-04-19 15:35:21
Wow, I could never have predicted that I would cause such a principled exchange, I was only having a laugh :)
Dems
2009-04-19 16:43:02