Josh (the blog)

I’ve delivered simple, clear and easy-to-use services for 20 years, for startups, scaleups and government. I write about the nerdy bits here.


@joahua

Cool MySQL function

I just discovered that MySQL has a mathematical function called RAND(). It serves two purposes, one of which was relevant to me, the other is just nifty to know.

You can use RAND() as a random number generator (although it’s probably not terribly random) simply by using the syntax

SELECT RAND();

or with a seed value between the parentheses

SELECT RAND(1234);

or (and this is the cool one for me) like this

SELECT * FROM tbl_name ORDER BY RAND();

to retrieve results in a random order.

Nifty, hey?

More info can be found on the MySQL Reference Manual Mathematical Functions page