select a random record

Started by Nonameover 24 years ago4 messagesgeneral
Jump to latest
#1Noname
bj@zuto.de

Hi!

I'm currently thinking about building a completely database driven
jukebox. It will integrate with an already existing (well kind of) library
for my music files (and more).

This jukebox should offer a random play mode. It should take into account
when the title was played the last time. My Idea is to submit the query
returning the records ordered by their last time of play, ignore a random
number of records, fetch one record and drop the result set.

I'm wondering wether there is a more elegant way to fetch a random record
from a result.

Rainer

--
KeyID=759975BD fingerprint=887A 4BE3 6AB7 EE3C 4AE0 B0E1 0556 E25A 7599 75BD

#2Andre Schnabel
a_schnabel@t-online.de
In reply to: Noname (#1)
Re: select a random record

----- Original Message -----
From: "Rainer Clasen" <bj@zuto.de>
Subject: [GENERAL] select a random record

...

This jukebox should offer a random play mode. It should take into account
when the title was played the last time. My Idea is to submit the query
returning the records ordered by their last time of play, ignore a random
number of records, fetch one record and drop the result set.

I'm wondering wether there is a more elegant way to fetch a random record
from a result.

Rainer

The following select should do it:
SELECT title_name FROM titles
ORDER BY last_played DESC
LIMIT 1
OFFSET ( floor ( random () * 20);

#3Noname
bj@zuto.de
In reply to: Andre Schnabel (#2)
Re: select a random record

On Sat, Sep 01, 2001 at 09:07:04AM +0200, Andre Schnabel wrote:

The following select should do it:

Thanks! This is perfect. And I'm astonished how easy it is.

Rainer

--
KeyID=759975BD fingerprint=887A 4BE3 6AB7 EE3C 4AE0 B0E1 0556 E25A 7599 75BD

#4Tod McQuillin
devin@spamcop.net
In reply to: Noname (#1)
Re: select a random record

On Sat, 1 Sep 2001, Rainer Clasen wrote:

This jukebox should offer a random play mode. It should take into account
when the title was played the last time. My Idea is to submit the query
returning the records ordered by their last time of play, ignore a random
number of records, fetch one record and drop the result set.

I'm wondering wether there is a more elegant way to fetch a random record
from a result.

Sure, well, try this:

SELECT whatever FROM whereever WHERE whatever ORDER BY random() limit 5;

replace whatever and whereever with your selection criterie -- the order
by random() will randomise the results and limit 5 chooses only the first
5.
--
Tod McQuillin