SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Crazy Webmaster Cr0CX's Avatar
    Join Date
    Dec 2003
    Location
    WWW
    Posts
    428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    randomize 3 selected rows..

    Hello, I would like to select one of the 3 users with most points on my website, and randomly show one of them... i try this: but it just randomizes, and then shows someone based on the point

    SELECT * FROM `users` WHERE `active`='Y' order by `points`, rand() DESC

    I want to:
    Select the 3 users with the most points
    Randomly choose one
    Have the query store just 1..

    is it possible?
    help..
    Run a site? over 1,000 uniques a day? PM me for link exchange.

  2. #2
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have to shift DESC from RAND() to Points.

    This should work:

    SELECT * FROM `users` WHERE `active`='Y' order by `points` DESC, rand() LIMIT 1

  3. #3
    Crazy Webmaster Cr0CX's Avatar
    Join Date
    Dec 2003
    Location
    WWW
    Posts
    428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi... thank you! But this won't give me 1 random out of the 3 with most points..
    Run a site? over 1,000 uniques a day? PM me for link exchange.

  4. #4
    reads the ********* Crier silver trophybronze trophy longneck's Avatar
    Join Date
    Feb 2004
    Location
    Tampa, FL (US)
    Posts
    9,854
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Code mysql:
    select *
      from (select *
              from users
             where active = 'Y'
            order
                by points desc
             limit 3) dt
    order
        by rand()
     limit 1


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •