• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Returning A Random Record

Guest
Sep 26, 2008 Sep 26, 2008

Copy link to clipboard

Copied

I want to cfquery a random record from a table. I can't randrange the key field because some keys are not there. I've seen a suggestion to load the keys into a table, grab a random instance, then query with that key but that seems like a long way around to me. There could be thousands of record in this table.

Any ideas? Thanks!
TOPICS
Advanced techniques

Views

223

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Deleted User
Sep 26, 2008 Sep 26, 2008
only access, no sql.
this is the code i am using.

<cfquery name="xxxx" datasource="dddd">
SELECT * FROM zzzz
</cfquery>

<cfset randomrow = randrange(1,xxxx.recordcount)>

<cfoutput query="xxxx" startrow=#randomrow# maxrows=1>
</cfoutput>

Votes

Translate

Translate
New Here ,
Sep 26, 2008 Sep 26, 2008

Copy link to clipboard

Copied

if you are using mssql:
select top 1 *
from your_table_name
order by newid()

google newid() and you can learn the details under the hood.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Sep 26, 2008 Sep 26, 2008

Copy link to clipboard

Copied

LATEST
only access, no sql.
this is the code i am using.

<cfquery name="xxxx" datasource="dddd">
SELECT * FROM zzzz
</cfquery>

<cfset randomrow = randrange(1,xxxx.recordcount)>

<cfoutput query="xxxx" startrow=#randomrow# maxrows=1>
</cfoutput>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation