Page 1 of 1

Random sorting

Posted: Sun Jan 18, 2004 10:46 pm
by d3ad1ysp0rk
Been searching forever (google, mysql.com, etc etc), and my question is so easy I can't believe I haven't found it yet..

I want to pick a random list of values from my table, so, is there any sql like this:
"SELECT * FROM `table` ORDER BY rand";

i believe thats what it was, im just not sure.. so is there a "random" order by clause?

thanks.

Re: Random sorting

Posted: Mon Jan 19, 2004 12:08 am
by microthick
LiLpunkSkateR wrote:Been searching forever (google, mysql.com, etc etc), and my question is so easy I can't believe I haven't found it yet..

I want to pick a random list of values from my table, so, is there any sql like this:
"SELECT * FROM `table` ORDER BY rand";

i believe thats what it was, im just not sure.. so is there a "random" order by clause?

thanks.
You've almost got it.

For MySQL:

select * from table order by rand()

You can also limit to however many random records you want.

For example, for a random tip of the day:

select tipid, tiptitle, tipbody from tips order by rand() limit 1


For MS-SQL Server, it's very similar:

select * from table order by newid()

Or, again for the tip of the day:

select top 1 tipid, tiptitle, tipbody from tips order by newid()