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

Returning A Random Record

Guest
Sep 26, 2008 Sep 26, 2008
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
265
Translate
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>
Translate
New Here ,
Sep 26, 2008 Sep 26, 2008
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.
Translate
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
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>
Translate
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