开发者

Mysql Weight based selection

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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜