If you're using mySQL then you can do this:

SELECT *
FROM  images
ORDER BY RAND()
LIMIT 1

Random every time.

<!----------------//------
andy matthews
web developer
ICGLink, Inc.
[EMAIL PROTECTED]
615.370.1530 x737
--------------//--------->

-----Original Message-----
From: Jim Davis [mailto:[EMAIL PROTECTED]
Sent: Wednesday, September 28, 2005 11:56 PM
To: CF-Talk
Subject: RE: Every once in a while, I feel smart. A story of displaying
a single random image.


> -----Original Message-----
> From: Taco Fleur [mailto:[EMAIL PROTECTED]
> Sent: Wednesday, September 28, 2005 11:47 PM
> To: CF-Talk
> Subject: RE: Every once in a while, I feel smart. A story of displaying a
> single random image.
>
> Hmm, sorry to break your bubbles but both solutions are very inefficient.
>
> The best thing to do is (in MS SQL)
>
> SELECT TOP 1 *
> FROM yourTable
> ORDER BY NEWID()
>
> That will give you a random record every time and you are not transferring
> a
> whole record set over the network.

Well... it's not really a random value (but close enough for practical
purposes).  ;^)

But forgetting that I think we're both making assumptions about the usage.
I was under the impression from the original poster that this random image
would be displayed a lot - so using a cached query and doing what I (or the
original poster) suggested would be faster by far than forcing the database
to generate and sort GUIDS for every image every time you want an image.

It's really the old argument of "where" - do you do this in CF or in the
database.  All the solutions presented will work - but some work better
depending on how often you use them and how much you can cache.

I was under the assumption that the album application would be making use of
the query information quite a bit.  It would probably be on hand cached some
place.  If that's the case it's silly to make a round trip to the database
to fetch random record when you have the data already.

But if the data's not be used elsewhere on the page or is not cached and
you're really only doing the query for a single random image then your
solution makes more sense.

Jim Davis





~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Logware (www.logware.us): a new and convenient web-based time tracking 
application. Start tracking and documenting hours spent on a project or with a 
client with Logware today. Try it for free with a 15 day trial account.
http://www.houseoffusion.com/banners/view.cfm?bannerid=67

Message: http://www.houseoffusion.com/lists.cfm/link=i:4:219601
Archives: http://www.houseoffusion.com/cf_lists/threads.cfm/4
Subscription: http://www.houseoffusion.com/lists.cfm/link=s:4
Unsubscribe: 
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.4
Donations & Support: http://www.houseoffusion.com/tiny.cfm/54

Reply via email to