Get a Quote Today!   |   + 1 (301) 424 3903




 
  Community
 
Need Help?
Visit our Training page
Request a Quote or Consultation
SIGN UP FOR THE TERATECH NEWSLETTER
 

ColdCuts

Cut: Returning random records
Cut#: 217 
Cut Date: 04/11/2001 
Cutter: Fred T. Sanders 
Topic: SQL 
Overview: If you need to return X number of random records from a SQL Server datasource, and the table you want to return them from has a numeric primary ID, why not let the sql server do the work??

In this example, I want to return 5 random categories.

 

Cut Code:
<cfquery name="qryRandomCategories" datasource="#request.site.MainDSN# dbtype="ODBC">
SELECT DISTINCT ID, Name
FROM tblCategories
WHERE ID = ROUND(RAND() * 264, 0) + 1)
OR ID = ROUND(RAND() * 264, 0) + 1)
OR ID = ROUND(RAND() * 264, 0) + 1)
OR ID = ROUND(RAND() * 264, 0) + 1)
OR ID = ROUND(RAND() * 264, 0) + 1)
</cfquery>