Get Random Record Based on Weight


/ Published in: MySQL
Save to your folder(s)

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


Copy this code and paste it in your HTML
  1. -- Must have as field named 'weight'
  2. SELECT * FROM `table` ORDER BY RAND() * (1 / `weight`)

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.