Jens Törnell
Jens Törnell

Reputation: 24768

MySQL - Order by number of chars

I have the most simple SQL

SELECT * FROM words

It has 13000 words (varchar). I need to get the longest word first in my output. I guess it might be possible with the WHERE command?

Alternative

If it doesn't work like that, is there a smart way to sort my output array so it will order it by the longest word first (in the 'word'-column). It looks like this (in the output loop)?

$output_array[$row['word']] = $row['another_word'];

Upvotes: 3

Views: 2261

Answers (2)

tomk
tomk

Reputation: 672

select * from words order by len(my_field) desc;

Upvotes: 0

Gumbo
Gumbo

Reputation: 655309

Ordering the words by its length should do it:

SELECT *
FROM words
ORDER BY LENGTH(word) DESC

Upvotes: 7

Related Questions