SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: how to do?

  1. #1
    SitePoint Member
    Join Date
    May 2008
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to do?

    Hello i have a small problem about a code.
    i have a table vacations where i have about 200 offers and i want to get some random offers in a table but i want only to get a picture title subtitle description. can someone give me an ideea how could i do this ? maybe an example ? thank you very much for you help

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,347
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    look in the documentation for the RAND() function
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Member
    Join Date
    May 2008
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937 View Post
    look in the documentation for the RAND() function
    i was thinking about using Rand(); but a friend who has allmost same random function on his web page.. told me not to use.. is that's i don't know what to do ..

  4. #4
    secure webapps for all Aleksejs's Avatar
    Join Date
    Apr 2008
    Location
    Riga, Latvia
    Posts
    755
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you want to get only title, picture and description, then your query would look like:
    SELECT title, picture, description FROM vacations

    As regarding to extracting random records, there are several options:
    1) add "ORDER BY RAND() LIMIT 10" if you want 10 random records
    2) generate say 30 (increase this number if necessary) random numbers that probably are in range of record IDs and use: WHERE id in (N1,N2,N3,...,N30) LIMIT 10 - this way you can avoid speed penalty of using ORDER BY RAND() (because index is not used when ordering by rand()). The point is that with high probability your generated number range will contain at least 10 valid ID numbers. We had similar discussion in php.lv forum recently.

  5. #5
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,347
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by Aleksejs View Post
    2) generate say 30 (increase this number if necessary) random numbers that probably are in range of record IDs and use: WHERE id in (N1,N2,N3,...,N30) LIMIT 10
    this is an excellent approach, simply excellent

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


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
  •