SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    MySQL Random Row

    Random MySQL Row...
    I see that may be a big problem with a lot of items.

    Code MySQL:

    So lets say you do, this:
    Wouldn't this be a problem if you had ID's like 1, 2, 3, 4, 7, 9, 12 -- You are missing some?
    Code MySQL:
    SELECT 
         id 
    FROM 
         items 
    WHERE id >= (
         SELECT FLOOR( MAX(id) * RAND()) 
         FROM items
     ) 
    ORDER BY 
         id 
    LIMIT 1;

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,494
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by JREAM View Post
    So lets say you do, this:
    Wouldn't this be a problem if you had ID's like 1, 2, 3, 4, 7, 9, 12 -- You are missing some?
    Why should it be a problem? Did you try it?

  3. #3
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,215
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    it's a problem because 12 will be returned more frequently than it would be if the algorithm were truly random
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  4. #4
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey thanks for the replies, I have a hard time finding my posts on the main page, HAHA.
    Ill test some stuff out with the randomizing though today or tomorrow

  5. #5
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,494
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by JREAM View Post
    Hey thanks for the replies, I have a hard time finding my posts on the main page, HAHA.
    On the forum main page, if scroll towards the end, it'll show your recent postings. And there is a link to show all your postings.


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
  •