mercredi 23 octobre 2019

SQL select statement with random number

I'm currently creating some type of quiz website.

My plan: Creating a random number in my sql statement to get a random question from my table.

E.g. every question have a numeric id -> creating random number (max number = records in my table) -> select random question by id

I tried the followed statement:

SELECT *
FROM question
WHERE ID = (SELECT FLOOR(RAND() * (SELECT COUNT(ID) FROM question)) + 1);

My problem: Sometimes I got no result, sometimes I got two results and sometimes it worked as planed.

If I try the SELECT FLOOR etc. on it's own, the random number works perfectly.

Any suggestions? Thank you in advance my friends!




Aucun commentaire:

Enregistrer un commentaire