latest reactions

spider
One way is to google for page which cont …
andy
you can find this tool here http://andy. …
Tjarko
Eej Walker, that's pretty cool, I will c …
Walker
Thanks for posting your thoughts. I modi …
Albert
Thanks !! …

Clicky Web Analytics
Use OpenDNS
ZERIGO DNS

Ultimate dns hosting based on pre-defined templates, DNS redirection and lot's more.

mxna feeds

CF101 Archive: September 2007 Object Oriented PizzaJob Opening for ColdFusion Developer at Arizona State UniversityMy New Experience Getting ColdFusion Builder 2 Console WorkingCF101 Archive: February 2007 Data Table GatewaysJob Opening for Sr. ColdFusion Developer in CA/ San Fernando ValleyColdFusion Supports HTTP Verbs PUT And DELETE (As Well As GET And POST)Best wishes for the new FuseboxCF101 Archive: January 2007 Data Access ObjectsOpen Source Conference For CFML DevelopersSkype Blocking Web Services with IIS, ColdFusion & RailoCF101 Archive: October 2006 Five Cool Things with ColdFusionGenerate a tag cloud from an RSS feed with ColdFusionColdFusion MeetUp: Cloudy with a Chance of Tests, with Mike HenkeReminder - Adobe CookbooksCF101 Archive: September 2006 A first look at FusionDebug

All files are strictly confidential: all information is classified.
© Copyright 2002 - 2012 mximize.com.
All right reserved.

Visit Carlos GallupaMXNA webfeed

How do I randomly show a query record?

The easiest way to do this is by the "ORDER BY newid()" statement within your SQL statement. Drawback is that you need SQL server with this.

SELECT TOP 1 *
FROM table
ORDER BY newid()

The query above will give you a randomly choosen record each time.

Another way of doing this is by randomly displaying a record from the queryset you have.. you can do that like this (if you use access or some other database)

<cfquery name="rs" datasource="#dsn#">
  SELECT *
  FROM table
</cfquery>

<cfset displayRow = randRange(1,rs.recordcount)>
<cfoutput query="rs" startrow="#displayRow#" maxrows="1">
  -- show your record
</cfoutput>

11016 viewed | 5 opinion(s)  | del.icio.us | Digg it | Tjarko @ 13/09/05 10:06 cet


Reactions:

Bryan F. Hogan wrote.... (mail)

Even easier and cross DB.

<cfset displayRow=randRange(1, rs.recordCount)>
<cfdump var="#rs.columnName[variables.displayRow]#">

Remember no selecting * in sql.

13 September 2005 11:27 cet  

Tjarko wrote.... (site)

True. That's what I use in all my other examples. And the * is just me being lazy in my example writing.

13 September 2005 12:55 cet  

Tom Nunamaker wrote.... (mail)

You can use * for developing, but it adds overhead to the DB to lookup the field names of course.A nice trick in Query Analyzer for SQL server to get all of the fields in your table is to open the "columns" folder and drag the folder icon into the working area of Query Analyzer.That will display a comma delimited listing of all of the fields in that table.Copy and paste into your CFQUERY SQL statements (assuming you need every field).

I would question why you would return every single record in your table just to pick a random one.If the table is large, that's a LOT network and processing overhead you don't need.I would think using two queries to get the primary keys, pick a random record, and then querying to get the details of the record would be much more efficient.

16 September 2005 16:36 cet  

Tjarko wrote.... (site)

@Tom: Look at the first example.. it will only return 1 row totally random.. the second example is just that.. an example of how you could do it with let's say a totally different database.

here are examples like the first one for different databases http://www.petefreitag.com/item/466.cfm

20 September 2005 18:43 cet  

Aminiasi wrote.... (site)

Hey Tjarko the link you gave was helpful!!!!

22 November 2009 9:28 cet  

Leave your comment

Your name


Your url/website/link/email....


Some room for your reaction is placed here..



The answer to the ultimate question is?? (42 ;-))




URL en mail addresses are translated for you... life sometimes is that simple!!