Displaying random item but with extra weight?

Discussion in 'PHP' started by Python, Mar 16, 2006.

  1. #1
    Lets say, just for example I have 10 records in a database table which has the fields name and count...

    The content of the count field is a numeric value where each record is different...

    Now what I want to do is to select a random record from this database but the higher the count value is the more chance they have of being displayed...

    For example if the 1st record had count of 1000 and 8th record had count of 100 - then it was run 10 times.. i would expect the majority to be the 1st record but not all...

    So basically Im adding more weight to the higher number...

    How can I do this?

    Thanks
     
    Python, Mar 16, 2006 IP
  2. Slapyo

    Slapyo Well-Known Member

    Messages:
    266
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #2
    Obviously, don't select * unless you absolutely need every column returned. But this might work. You can also remove the limit 0,1 if you want to return more than one record. The higher the count the more weighted the row is, the more often it should show up.

    select * from tbl_mytable order by rand()*(1/count) limit 0,1
    Code (markup):
     
    Slapyo, Mar 16, 2006 IP
  3. Python

    Python Well-Known Member

    Messages:
    680
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    120
    #3
    Thanks :D

    Will try it out in a bit
     
    Python, Mar 16, 2006 IP