Return to Snippet

Revision: 72245
at June 14, 2017 06:22 by apphp-snippets


Initial Code
-- Must have as field named 'weight'
SELECT * FROM `table` ORDER BY RAND() * (1 / `weight`)

Initial URL
http://www.apphp.com/index.php?snippet=mysql-get-random-record-based-on-weight

Initial Description
This snippet allows you to get random record from given table by weight. To run it, it must have a field named "weight". The more "weight" is assigned, the more often the record is selected. The value of "weight" should be an integer between 1 and 10 (or anything else).

Initial Title
Get Random Record Based on Weight

Initial Tags
mysql

Initial Language
MySQL