Organizational Research By

Surprising Reserch Topic

Experts Most Trusted Topic


sql select one row randomly but taking into account a weight


sql select one row randomly but taking into account a weight  using -'sql,mysql,select,random,weight'

I'm using MySQL.
I have a table which looks like that:

id: primary key
content: varchar
weight: int


What I want to do is randomly select one row from this table, but taking into account the weight. For example, if I have 3 rows:

id, content, weight
1, "some content", 60
2, "other content", 40
3, "something", 100


The first row has 30% chance of being selected, the second row has 20% chance of being selected, and the third row has 50% chance of being selected.

Is there a way to do that ? If I have to execute 2 or 3 queries it's not a problem.

Thanks in advance.
    

asked Oct 13, 2015 by balvant maurya
0 votes
5 views



Related Hot Questions



Government Jobs Opening


...