Zufällige Zeile aus Datenbank lesen

January 24th, 2010, 20:55H · Topics: Uncategorized · Print

Da ich immer wieder drüber stolpere:

Eine zufällige Zeile liest man mit MySQL wie folgt:

SELECT * FROM table ORDER BY RAND() LIMIT 1

Für Microsoft SQL Server verwendet man

SELECT TOP 1 * FROM table ORDER BY NEWID()

Arbeitet man mit IBM DB2 braucht man

SELECT *, RAND() as IDX FROM table ORDER BY IDX FETCH FIRST 1 ROWS ONLY

Mit PostgreSQL tut es

SELECT * FROM table ORDER BY RANDOM() LIMIT 1

Oracle will es als

SELECT * FROM ( SELECT * FROM table ORDER BY dbms_random.value ) WHERE rownum = 1

Wobei es bei jeder Datenbank auf die Performance geht ;-(

Tags: ,

Leave a Reply

Your email address will not be published. Your photo in comments, use Gravatar
Please include http://
Note: XHTML is allowed.

Subscribe to this comment feed via RSS

Software-Development & Support

Hyte Software Developing great software, and enhancing your experience in using OpenSource-Software