![]() |
MySQL problem
Hello
I have a question about MySQL. I have a column probability_index in my table and need to select a random row, but the higher the probability_index is, the higher chance is for the row to be selected. Any ideas? Thank you STTBoss |
you should be able to do
ORDER BY RAND(), probability_index DESC LIMIT 1 |
Quote:
|
you could generate a random number then do an if clause inside of the query and say if random number >= something then select a random result only within a certain range of probability_index values (the high end of these values) otherwise, you would select a random result without taking into account the probability_index value.
so... you generate a random number 0-9, and if that random number>1, which will happen 80% of the time, you would only select a random value from the top end of probability_index values. this would cause higher probability_index values to show up more often. |
Quote:
|
may need to use some code for it, like php or other
without knowing the values of probability index column, the first that comes to mind that it might get done is: create a temporary table insert into the temp table an id for each probability index difference - if it's 83, put it in 83 times - next one is 41, insert the id 41 times select id from temp table order by rand(); select * from main table where temp table id = main table id; maybe not elegant, but it's the first thing that came to mind without knowing if a mysql function exists to handle it easier |
Quote:
|
Quote:
|
Quote:
and fill the proper rows in this new table when inserts to original are done. look up the mysql bulk insert syntax, and do this in php :2 cents: |
Quote:
|
OK guys this was a real pain in the ass, but here is the solution (works pretty smoothly, I tested it ;)) The way I use it is a more complicated, I adjusted the code for posting to this thread.
First, I added two INT columns to the table - from_no and to_no Code:
mysql_query("ALTER TABLE table_name ADD COLUMN (from_no INT, to_no INT)") or die(mysql_error()); Code:
$from_no = '0'; Code:
$row = mysql_fetch_array(mysql_query("SELECT MAX(to_no) * RAND() AS rnd FROM table_name")); |
All times are GMT -7. The time now is 11:03 PM. |
Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123