Posted By

apphp-snippets on 06/14/17


Tagged

mysql random record


Versions (?)

Get Random Record Based on Weight


 / Published in: MySQL
 

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

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).

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

Report this snippet  

You need to login to post a comment.