SQL trick for ordering randomly by using NEWID()

Coldfusion 8This is a simple trick when you need to pull a query and order the records randomly.

<cfquery datasource="#application.dsn#" name="GetProducts" cachedwithin="#session.cacheTime#">
SELECT TOP 1 *
FROM tblProducts
ORDER BY NEWID()
</cfquery>

This code gets one random product from my database.

Comments
Damon Ledet's Gravatar Perfect timing, I was looking around just today to do this on the photo album I am creating at http://www.damonledet.com/photos
# Posted By Damon Ledet | 10/18/07 6:41 PM

BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.