I want to get a random row from my table by id.
My table:
ID|Word |Dificult|Category_id|
1 |'Dumb' |'Easy' | 3 |
2 |'Leopard'|'Medium'| 6 |
If my user selects a category and a difficult, I have pick a random word with user parameters, like:
idRaffle := raffle.id;
-- raffle.id = An id that postgres will bring me by some raffle function.
d := 'Easy';
c := 3;
select * from words where id=idRaffle and Dificult=d and Category_id=c
raffle.id
I don't know how to get that random row id (raffle.id
).
Note that it must follow the user's selection conditions.
Difficult = 'Easy' AND Category_id = 3
?ORDER BY random() LIMIT 1
is actually rather slow because it needs to scan and sort the whole table. Fine for small tables, impractical for big tables.