Re: Rand() Emulation

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wednesday 25 June 2003 00:19, Gerard Samuel wrote:
> 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
> in nature.
>
> 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
> random rows.

Here's a trick taken from Paul DuBois' MySQL book:

 SELECT ..., some_column * 0 + RAND() as rand_col FROM ... ORDER BY rand_col;

I've not tried it myself so YMMV.

-- 
Jason Wong -> Gremlins Associates -> www.gremlins.biz
Open Source Software Systems Integrators
* Web Design & Hosting * Internet & Intranet Applications Development *
------------------------------------------
Search the list archives before you post
http://marc.theaimsgroup.com/?l=php-db
------------------------------------------

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux