Howto query for random strings?

Possible duplicate:
Alerternative to MySQL Order by Rand ()

What is an efficient query method for random result sets in the following scenarios:

  • Choose one random line from many.
  • Select (at least) n random strings from many.
  • Select all rows in random order.

Particularly interested in MySQL, but may be a reason to try something else.

(The primary key is a solid integer AUTO_INCREMENT.)

+3
source share
1 answer

Edit: As noted by OMG Ponies: This does not scale at all. Thanks OMG.

Try using

ORDER BY RAND()

So...

SELECT * FROM `table` ORDER BY RAND() LIMIT 1
SELECT * FROM `table` ORDER BY RAND() LIMIT n
SELECT * FROM `table` ORDER BY RAND()
+2

Source: https://habr.com/ru/post/1734821/


All Articles