#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    1
    Rep Power
    0

    SELECT RANDOM with odds


    Hi, I would like to SLECT a random entry, so I use :
    [MYSQL]ORDER BY RAND() LIMIT 1[/MYSQL]

    But I want to get it according to "odds" ,
    I would like :
    80% of entry with label "blue" (8 chances up to 10 on each SELECT), and
    20% of entry with label "red" (2 chances up to 10 on each SELECT)

    How could I do ?

    Merci,
    Sylvain.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2007
    Location
    Purley, Surrey
    Posts
    248
    Rep Power
    37
    would this do the trick

    PHP Code:
    select from 
    ((select from TheTable
        where TheField
    ='blue'
        
    order by rand()
        
    limit 8)
    union
     
    (select from TheTable
        where TheField
    ='red'
        
    order by rand()
        
    limit 2)) dt 
    order by rand
    ()
    limit 1

IMN logo majestic logo threadwatch logo seochat tools logo