NYCPHP Meetup

NYPHP.org

[nycphp-talk] Need help randomly accessing records from a database

Dan Cech dcech at phpwerx.net
Thu Sep 1 09:35:51 EDT 2005


Try this:

SELECT *
FROM `sometable`
ORDER BY RAND()
LIMIT 1

Dan

Cliff Hirsch wrote:
> I need to randomly access records from a database and have come up with
> the following solution:
> 
> 1. Find max primary key id
> 2. Generate random # between 0 & max
> 3. Get record
> 
> This solution implies that the primary key value is 0...Max,
> sequentially without "holes".
> 
> But what it I delete records or want to "mask out" certain records? If I
> delete records, it's seems like the only method would be to reindex the
> database. And to mask, I'm lost other than using a 2nd database
> containing just the desired records with a join to the first.
> 
> Any ideas?
> 
> Cliff Hirsch



More information about the talk mailing list