> Ultimately, my goal is to emulate mysql's ->
> SELECT * FROM TABLE ORDER BY RAND() LIMIT (X)
> for other databases that do not support RAND().
Which are those?
> To me the idea above would work, but it hinges on if that rand column,
> doesn't have gaps.
Use your table's primary key, assuming there is one. This saves creating what
is otherwise unncessary data, but you'll still face "gaps" if records are
Did I help you? Want to show your thanks?
----- Original Message -----
From: "Gerard Samuel" <[EMAIL PROTECTED]>
To: "Doug Thompson" <[EMAIL PROTECTED]>
Cc: "Becoming Digital" <[EMAIL PROTECTED]>; "PHP-DB"
Sent: Tuesday, 24 June, 2003 12:19
Subject: Re: [PHP-DB] Rand() Emulation
Im probably not making myself clear.
Ultimately, my goal is to emulate mysql's ->
SELECT * FROM TABLE ORDER BY RAND() LIMIT (X)
for other databases that do not support RAND(). So using variations of
php's rand(), wouldn't make sense,
as it only picks one value out of a range of values, that are numerical
But here is an idea, that Im thinking about, but haven't gotten to the
code as yet.
1. In the tables that I want random values from, create a "rand"
column, that contains incremental numerical values for each row.
2. select the the maximum number from the rand column.
3. Assume that there are no gaps between 0 and this max number.
4. Create an array of numbers with values between 0 and max number.
5. Use array_rand() to "randomly" choose (x) values from the array
created in step 4. (I may choose maybe
a 1 or 2 values more than whats required, just in case of gaps between 0
and max number in step 3).
6. Use these randomly choosen values to select from the database as
So hopefully its a bit clearer what Im striving for.
To me the idea above would work, but it hinges on if that rand column,
doesn't have gaps.
If you see room for improvement, or have another idea, or if Im talking
gibberish then by all means.
Doug Thompson wrote:
>An incredible interpretation of
>If called without the optional min, max arguments rand() returns a
pseudo-random value between 0 and RAND_MAX. If you want a random number between
5 and 15 (inclusive), for example, use rand (5, 15).
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php