![]() |
![]() |
![]() |
||||
Welcome to the GoFuckYourself.com - Adult Webmaster Forum forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact us. |
![]() ![]() |
|
Discuss what's fucking going on, and which programs are best and worst. One-time "program" announcements from "established" webmasters are allowed. |
|
Thread Tools |
![]() |
#1 |
Confirmed User
Join Date: Jun 2004
Location: CZ
Posts: 225
|
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
__________________
TrafficAdept.com GET BETTER QUALITY TRAFFIC GET PAID MORE TO SELL TRAFFIC by DamageX and STTBoss ![]() |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#2 |
Confirmed User
Join Date: Feb 2006
Posts: 226
|
you should be able to do
ORDER BY RAND(), probability_index DESC LIMIT 1
__________________
icq: 199791893 holler at me |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#3 | |
Confirmed User
Join Date: Jun 2004
Location: CZ
Posts: 225
|
Quote:
__________________
TrafficAdept.com GET BETTER QUALITY TRAFFIC GET PAID MORE TO SELL TRAFFIC by DamageX and STTBoss ![]() |
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#4 |
Confirmed User
Join Date: Feb 2006
Posts: 226
|
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.
__________________
icq: 199791893 holler at me |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#5 | |
Confirmed User
Join Date: Jun 2004
Location: CZ
Posts: 225
|
Quote:
__________________
TrafficAdept.com GET BETTER QUALITY TRAFFIC GET PAID MORE TO SELL TRAFFIC by DamageX and STTBoss ![]() |
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#6 |
Beer Money Baron
Industry Role:
Join Date: Jan 2001
Location: brujah / gmail
Posts: 22,157
|
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
__________________
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#7 | |
Confirmed User
Join Date: Jun 2004
Location: CZ
Posts: 225
|
Quote:
__________________
TrafficAdept.com GET BETTER QUALITY TRAFFIC GET PAID MORE TO SELL TRAFFIC by DamageX and STTBoss ![]() |
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#8 | |
Confirmed User
Join Date: May 2005
Posts: 2,737
|
Quote:
|
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#9 | |
Confirmed User
Join Date: Jun 2005
Location: Irvine, CA
Posts: 2,442
|
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 ![]()
__________________
254342256
|
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#10 | |
Beer Money Baron
Industry Role:
Join Date: Jan 2001
Location: brujah / gmail
Posts: 22,157
|
Quote:
__________________
|
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#11 |
Confirmed User
Join Date: Jun 2004
Location: CZ
Posts: 225
|
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'; $result = mysql_query("SELECT id,probability_index FROM table_name") or die(mysql_error()); while ($row = mysql_fetch_array($result)) { mysql_query("UPDATE table_name SET from_no='$from_no', to_no='" . ($from_no + $row[probability_index]) . "' WHERE id=$row[id]") or die(mysql_error()); $from_no += $row[probability_index] + 1; } Code:
$row = mysql_fetch_array(mysql_query("SELECT MAX(to_no) * RAND() AS rnd FROM table_name")); $rnd = $row[rnd]; $row = mysql_fetch_array(mysql_query("SELECT id FROM table_name WHERE from_no<=$rnd AND to_no>=$rnd LIMIT 1")) or die(mysql_error());
__________________
TrafficAdept.com GET BETTER QUALITY TRAFFIC GET PAID MORE TO SELL TRAFFIC by DamageX and STTBoss ![]() |
![]() |
![]() ![]() ![]() ![]() ![]() |