SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist
    Join Date
    Sep 2004
    Location
    Oregon
    Posts
    445
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Finding Largest Number in Table

    I have a ID field, and I want to find the biggest number, then randomly select a number between 1 and the highest, WHERE another feild = something

    Ex

    $query = mysql_query("SELECT MAX(id) FROM competition WHERE score > 17");
    <-- Some script allowing you to randomly select a number from 1 to the highest number -->

    Ex. My Data in my table

    ID Email Score
    1 Blah@Blah.com 15
    2 Blah2@Blah.com 18
    3 Blah3@Blah.com 17
    4 Blah3@Blah.com 20

    I want it to randomly select a number from 1-4; but if their score isn't 18, 19, or 20 -- I want it to select a new number, or something in that sort. Understand?

    Thanks,
    -Joe

  2. #2
    SitePoint Guru dragonhawk's Avatar
    Join Date
    Apr 2002
    Location
    Melbourne
    Posts
    707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mysql_query('SELECT * FROM competition WHERE score > 17 ORDER BY RAND() LIMIT 1');

    But I remember reading somewhere that RAND() isn't that random (depending on which MySQL version you are using). If you find that it is not random, then the solution to that would be to run the below SELECT statement right before running that above SELECT statement (hope I'm clear)

    mysql_query('SELECT RAND(), RAND(), RAND()');

  3. #3
    Non-Member St.'s Avatar
    Join Date
    Nov 2004
    Location
    Cloud #9
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ORDER BY RAND()
    Does it work on mysql less than 4.xx version?

  4. #4
    SitePoint Guru dragonhawk's Avatar
    Join Date
    Apr 2002
    Location
    Melbourne
    Posts
    707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not too sure, but I think you might need to run mysql_query('SELECT RAND(), RAND(), RAND()'); to make it truly random for versions before 4.xx


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
  •