tags:

views:

115

answers:

1

Hi, I'm trying to select a number of elements from a mysql table based on their weight, example table being as follows.

Name | Weight
-------------
Bobo | 0.1
Jill | 0.3
Andy | 0.5
Dave | 0.9

Where weight is a float between 0 and 1.

What I would like to do is be able to select up to x rows based using the weight with a random factor whereby the results will be randomised based on the weight of the entry.

I'm using PHP right now to accomplish this and would like to be able to see how this can be done in MySQL.

I was thinking along the lines of this mysql semi-pseudeo code.

SELECT name, (weight calculation) as weight_calc ORDER by weight_calc LIMIT 0,x

A: 

Found the answer after browsing around here a while. Sods law I find the answer after I post the question.

SELECT * FROM table ORDER BY weight*random() DESC LIMIT x

Thanks hacker, link.

http://stackoverflow.com/users/95382/hacker

Furiam