jeudi 27 juillet 2017

Laravel select random rows from table based on another field

There is a words table with 20000 records:

ID      name      rank
1       word1     3 
2       word2     5019 
3       word3     12334 
4       word4     23
5       word5     544

I want to select 400 words randomly from this table but with this condition :

first 20 words: select 20 words randomly from words with rank between 1 and 1000

second 20 words: select 20 words randomly from words with rank between 1000 and 2000

And so on...

Do I have to do this in 20 separate queries? How? Is there a better way?

I am using laravel 5.4 and Mysql, Also a raw query suggestion would be appreciated. Thank you




Aucun commentaire:

Enregistrer un commentaire