mercredi 3 avril 2019

mysql Rand() function cause unexpected multirow results

When I try to get random row from table by id using RAND() function I get unexpected unstable results. The following query (where id column is primary key) returns 1, 2 or even more rows:

query and result

I tried next variant as well which produces same result:

SELECT id, word FROM words WHERE id = FLOOR(RAND() * 1000)

I found another solution for my task:

SELECT id, word FROM words ORDER BY RAND() LIMIT 1

But I want to know why MySQL behavior is so unexpected with using so elementary functionality. It scares me.

I experimented in different IDE with the same results.




Aucun commentaire:

Enregistrer un commentaire