MySQL re-calculates the value of a random number for each entry. let's see what happens:
SELECT id, (FLOOR(1 + RAND() * ((SELECT Count(id) FROM Test)))) rnd FROM Test
result:
'1', '4' '2', '12' '3', '10' '4', '12' '5', '8' '6', '1' '7', '5' '8', '10' '9', '9' '10', '11' '11', '9' '12', '12'
And now we impose our filter id=rnd on this, at the output of the line:
'9', '9' '12', '12'
Total 2 entries. It just so happened that at the time of selecting these id's from the rand() database, it gave the exact same value and the condition worked. If we are unlucky, rand() may not give a matching value for one line.
So it will be reliable:
SELECT id FROM Test, (select (FLOOR(1 + RAND() * ((SELECT Count(id) FROM Test)))) rnd) X WHERE id=rnd
In this case, MySQL first calculates (once) rand() , and then it will look for the record with the necessary id.
limit 1do not worry))) in general it would be nice to see it clearly, for example, you can put a link here on sqlfiddle.com - Alexey Shimanskylimit 1,countreplaced bymax, a=and<and thefloorremoved. Otherwise, it is impossible to get rid of empty samples, and the lines withid>count(id)do not shine - vp_arth