Topic awaiting preservation: PHP/MySQL -> order by rand() (Page 1 of 1) |
|
---|---|
Bipolar (III) Inmate From: switzerland |
posted 12-04-2002 14:37
I'm using MySQL version 3.23.53a on my server and the random sort function doesn't work for MySQL. |
Paranoid (IV) Inmate From: Milwaukee |
posted 12-04-2002 15:39
There's probably an SQL way of doing it, but you can circumvent your problem like this: if you're using PHP, perform your query, then feed your results into a multidimensional array. Then you can srand() to fire up the old random number generator, and shuffle() to randomize the array items. code: $query = "SELECT blah blah blah";
code: foreach ($random_result as $item)
code: for ($i = 0; $i < count($random_result); $i++)
|
Bipolar (III) Inmate From: switzerland |
posted 12-04-2002 17:28
Yeah, that's another possibility. But personnally I think my version i posted is better in performance, 'cuz I don't recieve all entries (because my query is limited to max. 15, sorry, I forgot to mention), and don't have to shuffle such a huge array. Another possibility would be, to make an array with all numbers from 1 to [row_num], then shuffle that array, and get the first 15 objects with the named ID's (actually row-numbers) |
Maniac (V) Mad Scientist From: 100101010011 <-- right about here |
posted 12-04-2002 18:34
hmm this worked perfect for me |
Paranoid (IV) Mad Scientist with Finglongers From: Germany |
posted 12-04-2002 21:36
To quote the Mysql Manual... |
Bipolar (III) Inmate From: switzerland |
posted 12-05-2002 08:49
hmm. I tried that too: |
Bipolar (III) Inmate From: switzerland |
posted 12-05-2002 08:57
I did a little bit of testing in PHPmyAdmin. The funny thing is, that it works for some tables, and it doesn't work for others (ie. the one I intended to use order by rand()). Is it possible, that it only works, if the table has at least x tables? |