faqts : Computers : Programming : Languages : PHP : Database Backed Sites : General

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

9 of 27 people (33%) answered Yes
Recently 2 of 10 people (20%) answered Yes

Entry

Whats the most efficient way to pull random rows from a table using php4 and mysql?

Apr 22nd, 2002 01:14
ditoiu cristian, James Jensen, Jeff Eidsness,


Since you have not specified the database platform, I assume that you 
are looking for a SQL solution.  Unfortunately, there is no way to use 
SQL to get a random record in a script.  I would use a methodology 
where the keys for the tables are saved in an array structure, sorted 
programmatically (using C or VB for example), and then used in a 
boilerplate query script:
SELECT * FROM TABLE_X WHERE TABLE_X.KEY = {variable}
If you just SELECT from a table, the primary key is used and sorted on 
that index (in most dbms's).
I can help you with a VB or PHP/Perl solution using MSSQL or ACCESS, if 
you want to discuss details.  A long time ago, I found some code that 
sorted an array in a random fashion and I have used the same technique 
in different environments.
----------
ditoiu cristian : 
really , there is a way , working with mysql to :) .
select field1,field2,rand() from table order by rand();