SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: ORDER BY RAND()

  1. #1
    SitePoint Evangelist
    Join Date
    Jun 2010
    Posts
    453
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    ORDER BY RAND()

    This code returns 34 rows:
    Code:
    SELECT plan.*, min(trkg.curwipprice_online) AS curwipprice_online, trkg.addr
    		FROM trkg
    		RIGHT OUTER JOIN plan
    		ON plan.cliorder = trkg.cliorder
    		WHERE (TIMESTAMP(NOW()) BETWEEN TIMESTAMP(plan.begin) AND TIMESTAMP(plan.end) )
    		GROUP BY plan.item_name DESC
    All I changed was GROUP BY... to ORDER BY RAND().
    Code:
    SELECT plan.*, min(trkg.curwipprice_online) AS curwipprice_online, trkg.addr
    		FROM trkg
    		RIGHT OUTER JOIN plan
    		ON plan.cliorder = trkg.cliorder
    		WHERE (TIMESTAMP(NOW()) BETWEEN TIMESTAMP(plan.begin) AND TIMESTAMP(plan.end) )
    		ORDER BY RAND()
    Now only 1 row is selected.

    Why?

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,246
    Mentioned
    59 Post(s)
    Tagged
    3 Thread(s)
    why? because you dropped the GROUP BY clause
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Evangelist
    Join Date
    Jun 2010
    Posts
    453
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    duh duh duh duhhhhhhh. Thanks again as usual. I value your help very much.

    I was proceeding from a memory that I couldn't combine a GROUP BY and an ORDER BY as in
    Code:
    SELECT plan.*, min(trkg.curwipprice_online) AS curwipprice_online, trkg.addr
    		FROM trkg
    		RIGHT OUTER JOIN plan
    		ON plan.cliorder = trkg.cliorder
    		WHERE (TIMESTAMP(NOW()) BETWEEN TIMESTAMP(plan.begin) AND TIMESTAMP(plan.end) )
    		GROUP BY plan.item_name DESC
    		ORDER BY RAND()
    Though, this appears that it produces the result I need.

    I have a new memory.

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,246
    Mentioned
    59 Post(s)
    Tagged
    3 Thread(s)
    regarding the TIMESTAMP functions, what is the datatype of your begin and end columns?

    because i think the WHERE condition will work fine without the functions
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    SitePoint Evangelist
    Join Date
    Jun 2010
    Posts
    453
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    They are timestamps so I changed to:
    Code:
    WHERE NOW() BETWEEN plan.begin AND plan.end
    which was successful. Thank-you very much for the tip. Looks like I don't need to convert to timestamps data that's already a timestamp.

    I love unexpected learning.

    Thanks for being so helpful r937.

  6. #6
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,014
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    Instead of using the ORDER BY RAND() , drop that bit and once you've got the result set into an array using whatever server-side language you're using, it'll be quicker. There is a thread somewhere (either in the PHP or the Database forum), I can't remember the thread title but the efficiency of different methods for getting a random result set were tested and compared including how well they scaled up (execution time) as the result sets got bigger.
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator


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
  •