Events order by startDate, EndDate - show single date events above date ranges

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hodgeman
    New Member
    • Feb 2008
    • 7

    Events order by startDate, EndDate - show single date events above date ranges

    Hello thescripts forums users...

    I've found a lot of answers to problems I've had in the past from this forum, and this is the first mySQL query issue I haven't been able to find an answer from anywhere so thought my time first post should be here.

    With that out the way...

    I have a website I've built for my day job rotoruaNZ.com/events that I'd like to improve upon.

    We have numerous events posted on our website that take place around the region and I've come across an issue that wasn't considered while development was taking place.

    All events have a compulsory start date field set and an optional end date for multiple consecutive day events.

    Problem comes around when I display the events ordered by start date.
    We are getting some long running exhibitions at our local museum and they run for months on end. Therefore they are staying at the top of the list until they expire or the end date has past.

    Current sql query (basically)
    Code:
    SELECT startDate, endDate, name FROM `events` WHERE publish=1 AND expired=0 ORDER BY startDate
    CURRENT RESULT - as of 2008-02-22 ====
    startDate - endDate - name
    =============== =============== ==
    2008-01-16 - 2008-03-15 - Te Huringa -Turning Points: Pakeha Colonisation & ...
    2008-01-16 - 2008-03-02 - From Under the Southern Cross – An Exhibition of...
    2008-01-16 - 2008-03-30 - Nickel Plated Machines:Tubula r Steel Furniture in ...
    2008-01-22 - 2008-04-04 - Women's Activator Series 2008
    2008-01-27 - 2008-05-25 - Rotorua Arts, Crafts and Produce Fair
    2008-02-19 - 2008-02-22 - Learning@School s
    2008-02-22 - 0000-00-00 - Hans Theessink
    2008-02-22 - 0000-00-00 - Searchlight Tattoo Street Parade
    2008-02-23 - 2008-02-24 - Searchlight Tattoo
    2008-02-23 - 2008-02-24 - NZO 24-hours N-Duro
    2008-02-23 - 0000-00-00 - Mayoral Bike Ride Challenge
    2008-02-23 - 0000-00-00 - Rotorua Community Hospice Garage Sale
    2008-02-24 - 0000-00-00 - Rotorua Tractor & Machinery Club Live Day
    2008-02-24 - 0000-00-00 - Soundshell Market

    What I'd like returned is any single day events returned from the current date onwards before events that have a endDate set even if there startDate is prior to the current date

    So something like:
    ORDER BY IF (startDate>curr entDate), IF (endDate>curren tDate) ....
    this is where my brain starts melting....lol
    Not sure how to structure it, this seams to be one of the most complex queries I've ever had to construct.

    But this is what we would want returned based on the same rows as above, ideally...


    IDEAL RESULT for 2008-02-22==========
    startDate - endDate - name
    =============== =============== ==
    2008-02-22 - 0000-00-00 - Hans Theessink
    2008-02-22 - 0000-00-00 - Searchlight Tattoo Street Parade
    2008-02-19 - 2008-02-22 - Learning@School s
    2008-02-23 - 2008-02-24 - Searchlight Tattoo
    2008-02-23 - 2008-02-24 - NZO 24-hours N-Duro
    2008-02-23 - 0000-00-00 - Mayoral Bike Ride Challenge
    2008-02-23 - 0000-00-00 - Rotorua Community Hospice Garage Sale
    2008-02-24 - 0000-00-00 - Rotorua Tractor & Machinery Club Live Day
    2008-02-24 - 0000-00-00 - Soundshell Market
    2008-01-16 - 2008-03-15 - Te Huringa -Turning Points: Pakeha Colonisation & ...
    2008-01-16 - 2008-03-02 - From Under the Southern Cross – An Exhibition of...
    2008-01-16 - 2008-03-30 - Nickel Plated Machines:Tubula r Steel Furniture in ...
    2008-01-22 - 2008-04-04 - Women's Activator Series 2008
    2008-01-27 - 2008-05-25 - Rotorua Arts, Crafts and Produce Fair

    I've been scratching my head for weeks trying to figure this out in between other projects.
    So any help with nutting this one out would be amazingly appreciated.
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    Concatenate the 2 dates and sort on it, e.g.
    Code:
    SELECT * FROM table ORDER BY CONCAT(startDate,endDate)
    Ronald

    Comment

    • hodgeman
      New Member
      • Feb 2008
      • 7

      #3
      Thanks Ronald,

      But that still retrieves the same results in the same order. Just because the dates are joined together doesn't make them ORDER BY any differently.

      UPDATED SQL =============== =====
      =============== =============== ==
      Code:
      SELECT startDate,endDate,CONCAT(startDate,endDate) as dates,name FROM `events` WHERE publish=1 AND expired=0 ORDER BY dates
      UPDATED RESULT (as of 25 Feb) ======
      startDate - endDate - CONCAT(startDat e,endDate) as dates - name
      =============== =============== ==
      2008-01-16 - 2008-03-02 - 2008-01-162008-03-02 - From Under the Southern Cross...
      2008-01-16 - 2008-03-15 - 2008-01-162008-03-15 - Te Huringa -Turning Points: Pakeh...
      2008-01-16 - 2008-03-30 - 2008-01-162008-03-30 - Nickel Plated Machines:Tubula r...
      2008-01-22 - 2008-04-04 - 2008-01-222008-04-04 - Women's Activator Serie...
      2008-01-27 - 2008-05-25 - 2008-01-272008-05-25 - Rotorua Arts, Crafts and...
      2008-02-27 - 0000-00-00 - 2008-02-270000-00-00 - Bike to Breakfast
      2008-02-28 - 0000-00-00 - 2008-02-280000-00-00 - Rotorua Marae Line Da...
      2008-02-29 - 0000-00-00 - 2008-02-290000-00-00 - Tykes on Trikes
      2008-03-01 - 0000-00-00 - 2008-03-010000-00-00 - Karaoke Club Performa...
      2008-03-01 - 0000-00-00 - 2008-03-010000-00-00 - Waikato Pistons vs Bay...
      2008-03-01 - 0000-00-00 - 2008-03-010000-00-00 - Dutch Market Day
      2008-03-01 - 0000-00-00 - 2008-03-010000-00-00 - Girl Guide biscuit sales
      2008-03-01 - 0000-00-00 - 2008-03-010000-00-00 - More FM Triwoman Series

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        But that still retrieves the same results in the same order. Just because the dates are joined together doesn't make them ORDER BY any differently.
        I am very sorry. I completely misinterpreted your question.

        Ronald

        Comment

        • mwasif
          Recognized Expert Contributor
          • Jul 2006
          • 802

          #5
          Hi hodgeman,

          Are you looking for something like below?
          [CODE=mysql]SELECT startDate, endDate, name , IF(startDate >= NOW(), 1, 0) start_date_crit eria,
          IF(endDate > '0000-00-00', 1, 0) end_date_criter ia FROM events
          WHERE publish=1 AND expired=0
          ORDER BY start_date_crit eria DESC, startDate, end_date_criter ia DESC[/CODE]

          Comment

          • hodgeman
            New Member
            • Feb 2008
            • 7

            #6
            Originally posted by mwasif
            Hi hodgeman,

            Are you looking for something like below?
            [CODE=mysql]SELECT startDate, endDate, name , IF(startDate >= NOW(), 1, 0) start_date_crit eria,
            IF(endDate > '0000-00-00', 1, 0) end_date_criter ia FROM events
            WHERE publish=1 AND expired=0
            ORDER BY start_date_crit eria DESC, startDate, end_date_criter ia DESC[/CODE]
            Yeah, that's getting closer.
            All events with an endDate!=0000-00-00 that have past their startDate are right at the bottom now instead of scattered through the results based on their endDate. It's a bit more usable now that they don't take up all the room at the top of the first page.

            But is there anyway to tweak this query further to order just those results by endDate with startDates that have past? But still keeping them in the overall order of startDate.

            Like.....

            Make a tmp field:
            if (startDate < NOW)
            startDate
            else if (endDate != 0000-00-00)
            endDate

            And order by that field - how could I do that? - would that work?

            Comment

            • hodgeman
              New Member
              • Feb 2008
              • 7

              #7
              Cracked It!!!

              Finally got it solved...

              [CODE="mysql"]SELECT startDate, endDate, IF(startDate >= NOW(), startDate, endDate) date_sort, name FROM events WHERE publish=1 AND expired=0 ORDER BY date_sort[/CODE]
              RESULT based on original results (first post) ======
              startDate - endDate - date_sort - name
              =============== =============== =========
              2008-02-22 - 0000-00-00 - 2008-02-22 - Hans Theessink
              2008-02-22 - 0000-00-00 - 2008-02-22 - Searchlight Tattoo Street Parade
              2008-02-19 - 2008-02-22 - 2008-02-22 - Learning@School s
              2008-02-23 - 2008-02-24 - 2008-02-24 - Searchlight Tattoo
              2008-02-23 - 2008-02-24 - 2008-02-24 - NZO 24-hours N-Duro
              2008-02-23 - 0000-00-00 - 2008-02-23 - Mayoral Bike Ride Challenge
              2008-02-23 - 0000-00-00 - 2008-02-23 - Rotorua Community Hospice Garage...
              2008-02-24 - 0000-00-00 - 2008-02-24 - Rotorua Tractor & Machinery Club Live...
              2008-02-24 - 0000-00-00 - 2008-02-24 - Soundshell Market
              2008-01-16 - 2008-03-15 - 2008-03-15 - Te Huringa -Turning Points: Pakeha ...
              2008-01-16 - 2008-03-02 - 2008-03-02 - From Under the Southern Cross – An...
              2008-01-16 - 2008-03-30 - 2008-03-30 - Nickel Plated Machines:Tubula r Steel...
              2008-01-22 - 2008-04-04 - 2008-04-04 - Women's Activator Series 2008
              2008-01-27 - 2008-05-25 - 2008-05-25 - Rotorua Arts, Crafts and Produce Fair

              Because all events that don't have an endDate set are automatically expired and unpublished once their startDate passes. I created the temp feild date_sort and set it's value to startDate if that was after NOW else set it to endDate, then ordered by that new field date_sort.

              The result is exactly what I was after!
              Thank you to all of you who helped and guided me in the right direction.

              Results can be found at http://www.rotoruanz.com/events

              Comment

              • ronverdonk
                Recognized Expert Specialist
                • Jul 2006
                • 4259

                #8
                I am glad you solved this yourself. Hope to be of (some) assistance next time. See you.

                Ronald

                Comment

                Working...