Apr 13, 2017

SQLite: showing a random record from a table

Random record

If many occasion developer want to get a random record(s) from a table, because they are programming a quizz, or a game - it turns out there is no native instructions for that in SQLite
but you can use this workaround:

SELECT
*, random() as R
FROM Table
ORDER BY R
LIMIT 1 ;

Why it works

It insert a random number (R) for each record and then using the order by there is going to be a selection of the smaller number (R) - you can even change the LIMIT to get more than one record - let say 3

SELECT *, random() as R FROM Table ORDER BY R LIMIT 3 ;

No comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • A little pseudo painting using ArtRage2 - "Spring" is the word !
  • What is it lately, this unexplainable need to change everything that works ? What about this idiom " if it ain't broke, don't ...
  • Hannah Wagner (better known by her stage name Miss Hannah Minx; born Oct. 1990) is an American vlogger known on Youtube for her Kawaii ...
  • After I gave a shot at ArtRage2 bundled with the Wacom tablet, I took the other cd that was in the box - and installed .... Photoshop Eleme...

AltStyle によって変換されたページ (->オリジナル) /