CodeCharge Studio
search Register Login  

Visual PHP Web Development

Visually Create Internationalized Web Applications, Web Reports, Calendars, and more.
CodeCharge.com

YesSoftware Forums -> Archive -> GotoCode Archive

 Random Retreival of database records

Print topic Send  topic

Author Message
jmccann
Posted: 09/11/2002, 2:32 PM

I want to build a table with the Primary Key assigned as an numeric ID when record is created.

Subsequent to this, I would then like to run a query that extracts a fixed number of random records. For example, the database has 1,000 records, I want to extract say 50 at random. The next time I run a query I want to extract another 50 records at random.

Any one any ideas how to build this?

Many thanks,

jim mccannt
Brent
Posted: 09/12/2002, 6:51 PM

You didn't say which database you're using, so I'll assume MySQL.

Try this query:

select * from table order by rand() limit 50;

The results are ordered randomly by rand() and the limit returns the first 50.
Each time this statement is run, another random group of records are selected.

Keep in mind the rand() is not an index column so it has to physically sort the
1000 records which isn't that big a deal because you're dealing with a small
number of records. If you had 1 million records, then sorting that many records
would be far too slow and you would have to write a PHP function to randomly select
rows using your rcd_id. This is not a simple task because you could have holes in
your rcd_id sequence and you would need to loop again until all 50 records were
retrieved.

Brent
jmccann
Posted: 09/13/2002, 1:51 AM

Thanks Brent,

I'm currently running with Access with ASP, but ultimatley will move to MYSQL still using ASP.

thanks,

jim mccann

   


These are Community Forums for users to exchange information.
If you would like to obtain technical product help please visit http://support.yessoftware.com.

Internet Database

Visually create Web enabled database applications in minutes.
CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


Powered by UltraApps Forum created with CodeCharge Studio
Copyright © 2003-2004 by UltraApps.com  and YesSoftware, Inc.