Reputation: 1698
I have a MySQL table named 'events' that contains event data. The important columns are 'start' and 'end' which contain string (YYYY-MM-DD) to represent when the events starts and ends.
I want to get the records for all the active events in a time period.
Events:
------------------------------ ID | START | END | ------------------------------ 1 | 2013-06-14 | 2013-06-14 | 2 | 2013-06-15 | 2013-08-21 | 3 | 2013-06-22 | 2013-06-25 | 4 | 2013-07-01 | 2013-07-10 | 5 | 2013-07-30 | 2013-07-31 | ------------------------------
Request/search:
Example: All events between 2013-06-13 and 2013-07-22 : #1, #3, #4 SELECT id FROM events WHERE start BETWEEN '2013-06-13' AND '2013-07-22' : #1, #2, #3, #4 SELECT id FROM events WHERE end BETWEEN '2013-06-13' AND '2013-07-22' : #1, #3, #4 ====> intersect : #1, #3, #4
Example: All events between 2013-06-14 and 2013-06-14 : SELECT id FROM events WHERE start BETWEEN '2013-06-14' AND '2013-06-14' : #1 SELECT id FROM events WHERE end BETWEEN '2013-06-14' AND '2013-06-14' : #1 ====> intersect : #1
I tried many queries still I fail to get the exact SQL query.
Don't you know how to do that? Any suggestions?
Thanks!
Upvotes: 19
Views: 165069
Reputation: 1
If anyone is searching for a situation when the current date is residing between two periods (start/end date) in Microsoft SQL, please find below
select id from campaign where (getdate() BETWEEN start_date AND end_date)
Upvotes: 0
Reputation: 129
SELECT *
FROM events
WHERE endDate >= @startDate AND startDate <= @endDate
For explanation refer to this diagram:
[startDate: 2020-10-01, endDate: 2020-20-01]
@startDate
and @endDate
to searchSo, in order to get overlapping dates by providing start and end date, endDate must be greater than @startDate and startDate must be less than @endDate.
Upvotes: 5
Reputation: 3138
In PHP and phpMyAdmin
$tb = tableDataName; //Table name
$now = date('Y-m-d'); //Current date
//start and end is the fields of tabla with date format value (yyyy-m-d)
$query = "SELECT * FROM $tb WHERE start <= '".$now."' AND end >= '".$now."'";
Upvotes: 0
Reputation: 35
EDIT: I've squeezed the filter a lot. I couldn't wrap my head around it before how to make sure something really fit within the time period. It's this: Start date BEFORE the END of the time period, and End date AFTER the BEGINNING of the time period
With the help of someone in my office I think we've figured out how to include everyone in the filter. There are 5 scenarios where a student would be deemed active during the time period in question:
1) Student started and ended during the time period.
2) Student started before and ended during the time period.
3) Student started before and ended after the time period.
4) Student started during the time period and ended after the time period.
5) Student started during the time period and is still active (Doesn't have an end date yet)
Given these criteria, we can actually condense the statements into a few groups because a student can only end between the period dates, after the period date, or they don't have an end date:
1) Student ends during the time period AND [Student starts before OR during]
2) Student ends after the time period AND [Student starts before OR during]
3) Student hasn't finished yet AND [Student starts before OR during]
(
(
student_programs.END_DATE >= '07/01/2017 00:0:0'
OR
student_programs.END_DATE Is Null
)
AND
student_programs.START_DATE <= '06/30/2018 23:59:59'
)
I think this finally covers all the bases and includes all scenarios where a student, or event, or anything is active during a time period when you only have start date and end date. Please, do not hesitate to tell me that I am missing something. I want this to be perfect so others can use this, as I don't believe the other answers have gotten everything right yet.
Upvotes: 1
Reputation: 5825
SELECT *
FROM events
WHERE start <= '2013-07-22' OR end >= '2013-06-13'
Upvotes: 4
Reputation: 13594
Here lot of good answer but i think this will help someone
select id from campaign where ( NOW() BETWEEN start_date AND end_date)
Upvotes: 15
Reputation: 109
If you would like to use INTERSECT option, the SQL is as follows
(SELECT id FROM events WHERE start BETWEEN '2013-06-13' AND '2013-07-22')
INTERSECT
(SELECT id FROM events WHERE end BETWEEN '2013-06-13' AND '2013-07-22')
Upvotes: 0
Reputation: 829
You need the events that start and end within the scope. But that's not all: you also want the events that start within the scope and the events that end within the scope. But then you're still not there because you also want the events that start before the scope and end after the scope.
Simplified:
Because point 2 results in records that also meet the query in point 3 we will only need points 1 and 3
So the SQL becomes:
SELECT * FROM events
WHERE start BETWEEN '2014-09-01' AND '2014-10-13'
OR '2014-09-01' BETWEEN start AND end
Upvotes: 22
Reputation: 37233
try this
SELECT id FROM events WHERE start BETWEEN '2013-06-13' AND '2013-07-22'
AND end BETWEEN '2013-06-13' AND '2013-07-22'
output :
ID
1
3
4
Upvotes: 0
Reputation: 23490
If I understood correctly you are trying to use a single query, i think you can just merge your date search toghter in WHERE
clauses
SELECT id
FROM events
WHERE start BETWEEN '2013-06-13' AND '2013-07-22'
AND end BETWEEN '2013-06-13' AND '2013-07-22'
or even more simply you can just use both column to set search time filter
SELECT id
FROM events
WHERE start >= '2013-07-22' AND end <= '2013-06-13'
Upvotes: 28
Reputation: 3096
SELECT id
FROM events
WHERE start <= '2013-07-22'
AND end >= '2013-06-13';
Or use MIN()
and MAX()
if you don't know the precedence.
Upvotes: 11