SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,777
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Random Sort Order on 2nd Column?

    The following MySQL query returns a listing of Articles for each Sub-Section of my website...
    Code SQL:
    SELECT ap.ds_subsection_slug, a.slug, a.heading, a.summary, a.image
    FROM article AS a
    INNER JOIN article_placement AS ap
    ON a.slug = ap.article_slug
    WHERE ap.sd_section_slug = 'finance'
    AND ap.ds_dimension_slug = 'featured-finance'
    ORDER BY ap.ds_subsection_slug, a.slug


    My goal is to "randomize" the order in which Articles are displayed in a given Sub-Section. (Hopefully this will encourage Users to read more Articles?!)

    Is there some way in MySQL, to return a result-set first sorted alphabetically by Sub-Section, but then sorted *randomly* by Article??

    Sincerely,


    Debbie

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,268
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by DoubleDee View Post
    Is there some way in MySQL, to return a result-set first sorted alphabetically by Sub-Section, but then sorted *randomly* by Article??
    i believe you could try RAND() as your second ORDER BY expression
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,777
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937 View Post
    i believe you could try RAND() as your second ORDER BY expression
    I stand corrected!! (I thought I did that yesterday and it failed. Who knows?!)

    Thanks!!


    Okay, so a follow-up question... (And this may be more one for the PHP group...)

    Ideally what I would like to do is "randomize" the order in which Articles (links) are displayed, AND I also would like to incorporate the concept of "weighting".

    For example, I want the article "Filing Dates to Remember" to show up 85% of the time COMBINED WITH "randomizing" which Articles appear in the "Tax-Season" sub-section.

    How would I go about that?

    My initial thought would be to add an "Article Weight" column, and then from there I'd have to incorporate that with maybe something like RAND(), but I don't know...

    Sincerely,


    Debbie


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
  •