469,623 Members | 1,683 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,623 developers. It's quick & easy.

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

hodgeman
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)
Expand|Select|Wrap|Line Numbers
  1. 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:Tubular 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@Schools
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>currentDate), IF (endDate>currentDate) ....
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@Schools
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:Tubular 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.
Feb 22 '08 #1
7 3077
ronverdonk
4,258 Expert 4TB
Concatenate the 2 dates and sort on it, e.g.
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM table ORDER BY CONCAT(startDate,endDate)
Ronald
Feb 22 '08 #2
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 ====================
================================
Expand|Select|Wrap|Line Numbers
  1. 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(startDate,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:Tubular...
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
Feb 24 '08 #3
ronverdonk
4,258 Expert 4TB
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
Feb 24 '08 #4
mwasif
801 Expert 512MB
Hi hodgeman,

Are you looking for something like below?
Expand|Select|Wrap|Line Numbers
  1. SELECT startDate, endDate, name , IF(startDate >= NOW(), 1, 0) start_date_criteria, 
  2. IF(endDate > '0000-00-00', 1, 0) end_date_criteria FROM events 
  3. WHERE publish=1 AND expired=0 
  4. ORDER BY start_date_criteria DESC, startDate, end_date_criteria DESC
Feb 25 '08 #5
Hi hodgeman,

Are you looking for something like below?
Expand|Select|Wrap|Line Numbers
  1. SELECT startDate, endDate, name , IF(startDate >= NOW(), 1, 0) start_date_criteria, 
  2. IF(endDate > '0000-00-00', 1, 0) end_date_criteria FROM events 
  3. WHERE publish=1 AND expired=0 
  4. ORDER BY start_date_criteria DESC, startDate, end_date_criteria DESC
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?
Feb 25 '08 #6
Finally got it solved...

Expand|Select|Wrap|Line Numbers
  1. SELECT startDate, endDate, IF(startDate >= NOW(), startDate, endDate) date_sort, name FROM events WHERE publish=1 AND expired=0 ORDER BY date_sort
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@Schools
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:Tubular 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
Feb 25 '08 #7
ronverdonk
4,258 Expert 4TB
I am glad you solved this yourself. Hope to be of (some) assistance next time. See you.

Ronald
Feb 26 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Shabam | last post: by
4 posts views Thread by Rob Johnson | last post: by
67 posts views Thread by PC Datasheet | last post: by
4 posts views Thread by nologo | last post: by
1 post views Thread by swethak | last post: by
reply views Thread by devrayhaan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.