Forum OpenACS Q&A: Re: Random Book off bookshelf

Posted by Sebastiano Pilla on
I didn't find how Lars has done it, but anyway I have something similar on my site: to pull a song at random from a database table (using PostgreSQL) the query is
select id, artist, album, title, artist_url, album_url
      from SONGS
      order by random()
      limit 1 offset 0
The key is the order by random() to achieve the random ordering and the limit 1 offset 0 clause to get only the first row of the results.

Of course if there are better ways to do it I would be happy to know.
Posted by Jarkko Laine on

Yes, that's exactly how it's done in Lars's as well as in random-photo script. You can leave 'offset 0' away though, since it's the default behaviour and it wouldn't really matter anyway from which place we took the random song.