Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select a random row from table, but with odds?

I have a table that describes a variety of objects in my system (ie. umbrella, boots, satchel, whatever). Each one of these objects needs to have a distinct prevalence or incidence. For example, the umbrella is rarer than the boots. Based on those factors, I need to randomly select a single object (including a blank or 'no object found') based on that incidence value.

Yikes. Make sense?

like image 859
Don Avatar asked Feb 01 '26 04:02

Don


1 Answers

SELECT * FROM some_table
WHERE (100*RAND()) > some_table.percent_probability
LIMIT 1

....and the probability of selection is stored in the percent_probability field.

C.

like image 174
symcbean Avatar answered Feb 03 '26 19:02

symcbean



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!