Reputation: 2445
i am trying to figure out a way to select all the records in a table and display them in a random order.
I have heard a lot about RAND()
like in this query
$sql = mysql_query("SELECT * FROM table ORDER BY RAND()");
But to my knowledge this just selects a random number of records like 20.
How would select all the records but just display them in a random order?
Thanks
Upvotes: 1
Views: 1128
Reputation: 16310
I am not much clear about your question though, you can get all records in random order by simply having following query:
SELECT * FROM table ORDER BY RAND()
Above query generates a random value for each row of the table, sorting that table according to these random values, and return according to the sorted row. Hence, you'll have all records with random order.
Upvotes: 1
Reputation: 6966
You'd be better off selecting all the entries and leaving the random display to php. The whole ORDER BY RAND()
requires a lot of resources. The ORDER BY RAND()
operation actually re-queries each row of your table, assigns a random number ID and then delivers the results.
$sql = mysql_query("SELECT * FROM table ORDER BY RAND() LIMIT 1");
But to my knowledge this just selects a random number of records like 20.
Wrong: This will select all records then rearrange them "randomly" and give you the first row: LIMIT 1
, just leave it out.
$sql = mysql_query("SELECT * FROM table ORDER BY RAND()");
Upvotes: 1
Reputation: 22812
Put your results in an array, then just shuffle()
it.
And get rid of mysql_*
functions, girls don't like them anymore.
Upvotes: 0
Reputation: 1153
That's just this:
SELECT * FROM table ORDER BY RAND()
By dropping the LIMIT 1
you can get all records sorted randomly ...
Upvotes: 0