Be Excellent To Each Other

And, you know, party on. Dude.

All times are UTC [ DST ]




Reply to topic  [ 11 posts ] 
Author Message
 Post subject: MySQL question - probably simple, can't think this morning
PostPosted: Tue Nov 26, 2013 13:25 
User avatar

Joined: 30th Mar, 2008
Posts: 14321
Location: Shropshire, UK
This is probably dead easy, but I just can't engage my brain this morning.

I have a table that looks something like the below (some fields omitted, but they aren't relevant)

Code:
+----+------------+------------+-------+
| id | startdate  | enddate    | flags |
+----+------------+------------+-------+
|  1 | 2014-05-17 | 2014-05-24 |     1 |
|  2 | 2014-05-22 | 2014-05-29 |     4 |
+----+------------+------------+-------+


Basically, I want to return the number of days in any given row that fall between a user specified date range.

So, for example, if the user enters a startdate of 2014-05-13 and an enddate of 2014-05-19, that will match row id 1, and the query should tell me that there are three days that match the user's date range (17th, 18th, 19th)

Is that even possible?


Top
 Profile  
 
 Post subject: Re: MySQL question - probably simple, can't think this morning
PostPosted: Tue Nov 26, 2013 13:30 
User avatar

Joined: 30th Mar, 2008
Posts: 16611
http://www.w3resource.com/mysql/date-an ... nction.php

Any good?


Top
 Profile  
 
 Post subject: Re: MySQL question - probably simple, can't think this morning
PostPosted: Tue Nov 26, 2013 13:50 
User avatar

Joined: 30th Mar, 2008
Posts: 14321
Location: Shropshire, UK
I think that'll just do the number of days between two dates, but not what I need which is the number of days that intersect between two different date ranges.

Also, from that link, why the hell are people rating MySQL functions? 8 out of 10 from 11 ratings.


Top
 Profile  
 
 Post subject: Re: MySQL question - probably simple, can't think this morning
PostPosted: Tue Nov 26, 2013 13:57 
SupaMod
User avatar
Commander-in-Cheese

Joined: 30th Mar, 2008
Posts: 49237
Select between should help you. Select between on the start date, select between on the end date. Any rows that appear in both sets are a match.

_________________
GoddessJasmine wrote:
Drunk, pulled Craster's pork, waiting for brdyime story,reading nuts. Xz


Top
 Profile  
 
 Post subject: Re: MySQL question - probably simple, can't think this morning
PostPosted: Tue Nov 26, 2013 14:17 
SupaMod
User avatar
Est. 1978

Joined: 27th Mar, 2008
Posts: 69686
Location: Your Mum
I think your example is wrong, as the dates you give aren't falling between anything.

But I think you want

Code:
SELECT DATEDIFF(startdate,enddate) AS num_days WHERE startdate > [USER_START_DATE] AND enddate < [USER_END_DATE]

_________________
Grim... wrote:
I wish Craster had left some girls for the rest of us.


Top
 Profile  
 
 Post subject: Re: MySQL question - probably simple, can't think this morning
PostPosted: Tue Nov 26, 2013 14:32 
User avatar

Joined: 30th Mar, 2008
Posts: 14321
Location: Shropshire, UK
I don't think that'll do it, that will give me the number of days between the startdate and enddate in the table.

The example I gave is correct, I must have worded it badly.

In the table is the range 2014-05-17 to 2014-05-24 (7 days)

User enters 2014-05-13 to 2014-05-19 (6 days)

What I need is a query that will take the User's date range, and tell me how many days in that range match up with days in the table range.

So the User's date range matches up with the table's range on 3 days - 2014-05-17, 2014-05-18 and 2014-04-19 (because those three days are on row 1, and are in the user's date range)

Does that make any more sense?

It shouldn't be too expensive to do it by fetching the matching records and then individually counting days that match using a PHP loop, as there will never be that many data sets to worry about - but I'd prefer to avoid it if I can.


Top
 Profile  
 
 Post subject: Re: MySQL question - probably simple, can't think this morning
PostPosted: Tue Nov 26, 2013 14:38 
SupaMod
User avatar
Commander-in-Cheese

Joined: 30th Mar, 2008
Posts: 49237
Oh, sorry - I was going for 'which rows can accomodate all the user's dates'.

_________________
GoddessJasmine wrote:
Drunk, pulled Craster's pork, waiting for brdyime story,reading nuts. Xz


Top
 Profile  
 
 Post subject: Re: MySQL question - probably simple, can't think this morning
PostPosted: Tue Nov 26, 2013 15:00 
User avatar
Heavy Metal Tough Guy

Joined: 31st Mar, 2008
Posts: 6586
I think you want

DATEDIFF(
GREATEST( user_start_date, row_start_date ),
LEAST( user_end_date, row_end_date )
)

I think that's right - the number of days between the later of the two start dates and the earlier of the two end dates.


Top
 Profile  
 
 Post subject: Re: MySQL question - probably simple, can't think this morning
PostPosted: Tue Nov 26, 2013 15:30 
SupaMod
User avatar
Est. 1978

Joined: 27th Mar, 2008
Posts: 69686
Location: Your Mum
Okay, try this.
Code:
SELECT DATEDIFF(GREATEST([USER_START_DATE], startdate), LEAST([USER_END_DATE], enddate)) AS num_days
WHERE [USER_START_DATE] < enddate AND [USER_END_DATE] > startdate


[edit]I think this one's a little cheaper

_________________
Grim... wrote:
I wish Craster had left some girls for the rest of us.


Top
 Profile  
 
 Post subject: Re: MySQL question - probably simple, can't think this morning
PostPosted: Tue Nov 26, 2013 16:08 
User avatar

Joined: 30th Mar, 2008
Posts: 14321
Location: Shropshire, UK
You beauties. Cheers.

//edit: Needed to multiply the result by -1 to get a positive number that I could work with, but it did the trick.


Top
 Profile  
 
 Post subject: Re: MySQL question - probably simple, can't think this morning
PostPosted: Tue Nov 26, 2013 16:19 
SupaMod
User avatar
Est. 1978

Joined: 27th Mar, 2008
Posts: 69686
Location: Your Mum
You could have just swapped the order of the params in the DATEDIFF, but it's hardly going to make a big difference to the speed of the thing.

_________________
Grim... wrote:
I wish Craster had left some girls for the rest of us.


Top
 Profile  
 
Display posts from previous:  Sort by  
Reply to topic  [ 11 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: JBR, The Greys and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search within this thread:
You are using the 'Ted' forum. Bill doesn't really exist any more. Bogus!
Want to help out with the hosting / advertising costs? That's very nice of you.
Are you on a mobile phone? Try http://beex.co.uk/m/
RIP, Owen. RIP, MrC. RIP, Dimmers.

Powered by a very Grim... version of phpBB © 2000, 2002, 2005, 2007 phpBB Group.