0

I am looking for a solution which gives me a random row from a hsql db back.

CREATE TABLE Playlist(
    id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
);

Any ideas?

UPDATE:

SELECT LIMIT 0 1 RAND(), p.name as foo
From Playlist p
ORDER BY foo

with this statement I get a random number back, but not a random playlist name.

TheLethalCoder
  • 6,875
  • 6
  • 31
  • 58
maximus
  • 10,204
  • 25
  • 85
  • 124

1 Answers1

2

You should go to How to request a random row in SQL?

It covers a quite a few options on how to do what you need.

SELECT p.name as foo
From Playlist p
ORDER BY RAND() LIMIT 1

If you are using Oracle, you need a subselect using Rownum instead of limit. see How do I limit the number of rows returned by an Oracle query after ordering?

Community
  • 1
  • 1
Peter Wooster
  • 5,824
  • 1
  • 25
  • 38
  • Thanks @bonCodigo, Note that the limit works well in MySQL, but not in oracle, there it gets much uglier. – Peter Wooster Jan 12 '13 at 15:07
  • 1
    Forgot to add the link.. ;) http://viralpatel.net/blogs/fetch-random-rows-from-database-mysql-oracle-ms-sql-postgresql-example/ – bonCodigo Jan 12 '13 at 15:45