Reputation:
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
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
Reputation: 2985
Try:
SELECT `word`
FROM `words`
WHERE STRCMP(SUBSTRING(`word`,1,1),'x') = 0
ORDER BY RAND()
LIMIT 0,1
Upvotes: 1