NYCPHP Meetup

NYPHP.org

[nycphp-talk] Random record from MySQL

Mark Armendariz nyphp at enobrev.com
Thu Mar 11 11:29:32 EST 2004


Not sure of the efficiency of rand, but I've been using it for a while..
Very useful.

Another way you could do it is to generate a random number from 0 to the
record count and use a limit query.

Mark

> -----Original Message-----
> From: talk-bounces at lists.nyphp.org 
> [mailto:talk-bounces at lists.nyphp.org] On Behalf Of Ophir Prusak
> Sent: Thursday, March 11, 2004 11:07 AM
> To: NYPHP Talk
> Subject: Re: [nycphp-talk] Random record from MySQL
> 
> 
> I've never seen this solution before. Very simple.
> I wondering though about the efficiency of this solution.
> 
> It seems to me that this assigns a random value to each row 
> and then orders by that row.
> Does the DB do a full table scan using this method?
> 
> If yes, this would be a very inefficient method for very large tables.
> 
> Ophir
> 
> 
> tom at supertom.com wrote:
> 
> >Jeff,
> >
> >Try:
> >
> >SELECT  * FROM table_name ORDER  BY RAND(  ) LIMIT 0 , 1
> >
> >Tom
> >www.liphp.org
> > 
> > 
> > 
> > 
> >***************************************************
> >What's Tom listening to right now?  Find out here:
> >http://www.supertom.com/current_track.php
> >
> >
> >
> >
> >-----Original Message-----
> >From: talk-bounces at lists.nyphp.org
> >[mailto:talk-bounces at lists.nyphp.org]On Behalf Of Jeff Siegel
> >Sent: Thursday, March 11, 2004 10:32 AM
> >To: NYPHP Talk
> >Subject: [nycphp-talk] Random record from MySQL
> >
> >
> >How can I pull a record at random from a MySQL table?
> >
> >Jeff Siegel
> >
> >_______________________________________________
> >talk mailing list
> >talk at lists.nyphp.org
> >http://lists.nyphp.org/mailman/listinfo/talk
> >
> >_______________________________________________
> >talk mailing list
> >talk at lists.nyphp.org
> >http://lists.nyphp.org/mailman/listinfo/talk
> >  
> >
> _______________________________________________
> talk mailing list
> talk at lists.nyphp.org
> http://lists.nyphp.org/mailman/listinfo/talk
> 
> 
> 




More information about the talk mailing list