Archive

Archive for March 20th, 2003

MySQL and Random

March 20th, 2003 2 comments

Yesterday, when writing some tests involving data stored in MySQL, I was quite impressed to discover that I could add random data to the table by doing:

UPDATE table SET column = FLOOR(8 * RAND()) + 1;

I was originally worried that this would pick a random value and then assign it to every row, but no, it picks a new random number each time.

Of course, I shouldn’t have been quite so surprised as I’ve long been a fan of picking a random row via:

SELECT * FROM table ORDER BY RAND() LIMIT 1

I’d just never thought through the implication that this must indeed be selecting a random value for each row in order to sort by it…

Tags: