user4438529
user4438529

Reputation:

MySQL: Selecting random things starting with a X letter

I am using this database: http://www.4redpixels.com/uploads/words.sql

I want to select a random word starting with a X (unknown) letter. How can I do that?

Upvotes: 1

Views: 82

Answers (2)

Gordon Linoff
Gordon Linoff

Reputation: 1269753

Actually, the best way to get words that start with a particular letter is to use like:

select w.word
from words w
where w.word like 'x%'
order by rand()
limit 1;

This can take advantage of an index on words(word). In addition, if the list is really long, there are better ways to get a random row than just order by rand().

Upvotes: 0

Daniel Waghorn
Daniel Waghorn

Reputation: 2985

Try:

SELECT `word`
FROM `words`
WHERE STRCMP(SUBSTRING(`word`,1,1),'x') = 0
ORDER BY RAND() 
LIMIT 0,1

Upvotes: 1

Related Questions