Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How come this random query doesn't work?

Posted on 2003-11-06
8
Medium Priority
?
338 Views
Last Modified: 2013-12-24
<CFQUERY name="q_GetImages" datasource="Rating">
  SELECT TOP 1 *
  FROM ImageIndex II
  WHERE II.Approved = 1 AND NOT EXISTS (SELECT 1 FROM RatingsTable RT WHERE RT.ImageId = II.ImageId)
  ORDER BY rnd([ImageID])
 </CFQUERY>

For some reason this isn't returning random records, why not?
0
Comment
Question by:Max_Davis
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 14

Expert Comment

by:Renante Entera
ID: 9698656
Is this line correct ?  
   WHERE II.Approved = 1 AND NOT EXISTS (SELECT 1 FROM RatingsTable RT WHERE RT.ImageId = II.ImageId)

If not change it with this one, if this is what you are referring :
   WHERE II.Approved = 1 AND NOT EXISTS (SELECT TOP 1 FROM RatingsTable RT WHERE RT.ImageId = II.ImageId)

Goodluck!
eNTRANCE2002 :-)
0
 
LVL 14

Expert Comment

by:Renante Entera
ID: 9698666
Ooops! Sorry, I forgot something.

It should be this way :

WHERE II.Approved = 1 AND NOT EXISTS (SELECT TOP 1 * FROM RatingsTable RT WHERE RT.ImageId = II.ImageId)

Regards!
eNTRANCE2002 :-)
0
 
LVL 14

Expert Comment

by:Renante Entera
ID: 9698701
Try this query:

<CFQUERY name="q_GetImages" datasource="Rating">
  SELECT TOP 1 *
  FROM ImageIndex II
  WHERE II.Approved = 1
  AND NOT EXISTS (SELECT TOP 1 * FROM RatingsTable RT WHERE RT.ImageId = II.ImageId)
  ORDER BY rnd([RT.ImageID])
</CFQUERY>

Perhaps this will return any records.
0
Simplify Your Workload with One Tool

How do you combat today’s intelligent hacker while managing multiple domains and platforms? By simplifying your workload with one tool. With Lunarpages hosting through Plesk Onyx, you can:

Automate SSL generation and installation with two clicks
Experience total server control

 
LVL 17

Expert Comment

by:anandkp
ID: 9698957
<CFQUERY name="q_GetImages" datasource="Rating">
  SELECT TOP 1 *
  FROM ImageIndex II
  WHERE II.Approved = 1 AND NOT EXISTS (SELECT * FROM RatingsTable RT WHERE RT.ImageId = II.ImageId)
  ORDER BY rnd([II.ImageID])
 </CFQUERY>
0
 
LVL 10

Expert Comment

by:substand
ID: 9699314
it's not returning random results because even though the sql spec calls for it to, two things might be happening:

1) your dbms probably just returns the first record in the db file
2) if the dbms is set up to actually find a random record instead of returning the first one, you have an index set up on one or more fields so it is returning based on the index.
0
 
LVL 1

Accepted Solution

by:
Wasistdas earned 1600 total points
ID: 9699614
Why not be simpler?

<CFQUERY name="q_GetImages" datasource="Rating">
  SELECT *
  FROM ImageIndex
</cfquery>
<cfif q_GetImages.recordcount>
<cfset random_factor=RandRange(1,q_GetImages.recordcount)>
<cfoutput query="q_GetImages" startrow="#random_factor#" maxrows="1">
[output]
</cfoutput>
</cfif>
0

Featured Post

Simplify Your Workload with One Tool

How do you combat today’s intelligent hacker while managing multiple domains and platforms? By simplifying your workload with one tool. With Lunarpages hosting through Plesk Onyx, you can:

Automate SSL generation and installation with two clicks
Experience total server control

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever sent email via ColdFusion and thought of tracking this mail to capture the exact date and time when the message was opened ?  If yes, then this article is for you ! First we need a table user_email with columns user_id , email , sub…
Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question