Results 1 to 7 of 7
  1. #1
    manik's Avatar
    manik is offline Om Shanti!
    Join Date
    Apr 2008
    Location
    Boston, MA
    Posts
    13,232
    Thanks
    745
    Thanked 729 Times in 564 Posts
    Blog Entries
    4
    Feedback Score
    4 (100%)

    Default Selecting Random Row from Database

    Select a random row with Microsoft SQL Server:
    SELECT TOP 1 column FROM table
    ORDER BY NEWID()


    Select a random row with MySQL:
    SELECT column FROM table
    ORDER BY RAND()
    LIMIT 1


    Select a random row with PostgreSQL:
    SELECT column FROM table
    ORDER BY RANDOM()
    LIMIT 1


    Select a random row with IBM DB2
    SELECT column, RAND() as IDX
    FROM table
    ORDER BY IDX FETCH FIRST 1 ROWS ONLY


    Select a random record with Oracle:
    SELECT column FROM
    ( SELECT column FROM table
    ORDER BY dbms_random.value )
    WHERE rownum = 1
    Free Classified Ads & BUSINESS/PROFESSIONAL SOCIAL NETWORK


  2. #2
    vikas1234 is offline Junior
    Join Date
    Jun 2009
    Posts
    433
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Feedback Score
    0

    Default

    Thanks manik ... the database article on webcosmo are really nice and useful

  3. #3
    kupika is offline Freshman
    Join Date
    Jul 2009
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Feedback Score
    0

    Default

    I also wanted know this...
    I am really thankful to Manik...
    I missed that class in college....
    Please visit our site for professionals Seo services http://www.seoservicesfirms.com

  4. #4
    brayanguy is offline Freshman
    Join Date
    Sep 2009
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Feedback Score
    0

    Default

    Hi friend.
    Thanks for the code.
    Its very useful.

  5. #5
    sinc is offline Junior
    Join Date
    Sep 2009
    Posts
    193
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Feedback Score
    0

    Default

    Hi Manik,

    Thanks for give this code its really use full for me.

  6. #6
    chamunda is offline Freshman
    Join Date
    Aug 2011
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Feedback Score
    0

  7. #7
    johnboucher040 is offline Freshman
    Join Date
    Mar 2013
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Feedback Score
    0

    Default

    to select random row from database on should follow this code:


    SELECT * FROM `table` ORDER BY RAND() LIMIT 0,1;

Similar Threads

  1. Replies: 0
    Last Post: 10-24-2008, 07:07 PM
  2. Replies: 0
    Last Post: 08-22-2008, 01:05 AM
  3. Replies: 0
    Last Post: 08-22-2008, 12:55 AM
  4. Replies: 0
    Last Post: 08-20-2008, 06:22 PM
  5. Replies: 0
    Last Post: 08-20-2008, 06:17 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Partners: BOSTON WEB DEVELOPER, LLC   |   WEBCOSMO CLASSIFIEDS