-1

I have a MySQL Datatable with an ID, two datetime and a boolean column.

Looks like this:

CREATE TABLE `Example` (
 `ID` int(11) NOT NULL AUTO_INCREMENT,
 `Date1` date NOT NULL,
 `Date2` date NOT NULL,
 `Answered` tinyint(1) DEFAULT NULL,
 PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

Now I have a startdate and an enddate, and I need a sql query which gives me that rows, where any day between the startdate and the enddate is between Date1 and Date2!

What works is that query, wich gives me those rows, where the Date1 is between the startdate and the enddate and Aswered = 1:

    $proof = "select * from Example where Date1 
    between '$startdate' and '$enddate' and Answered = 1";

But I dont know how to do that with all dates between two dates

Thanks for help!

DST
  • 49
  • 1
  • 8
  • Possible duplicate of [Determine Whether Two Date Ranges Overlap](http://stackoverflow.com/questions/325933/determine-whether-two-date-ranges-overlap) – Juan Carlos Oropeza Mar 30 '16 at 21:09
  • You should include sample data and desire output. Please read [**How-to-Ask**](http://stackoverflow.com/help/how-to-ask) And here is a great place to [**START**](http://spaghettidba.com/2015/04/24/how-to-post-a-t-sql-question-on-a-public-forum/) to learn how improve your question quality and get better answers. – Juan Carlos Oropeza Mar 30 '16 at 21:11
  • no thats not that what I am seaching for – DST Mar 30 '16 at 21:11
  • Show some sample data, and the expected result. – CBroe Mar 30 '16 at 21:12
  • I have no sample data and the expected result should be an array of datatablerows where any day between a startdate and an enddate is between Date1 and Date2 – DST Mar 30 '16 at 21:15
  • If you dont have sample data how will you test your query? Dont be lazy and prepare a proper question. – Juan Carlos Oropeza Mar 30 '16 at 21:20

4 Answers4

1

Ok based on the 2 answers above I solved my problem, this is the query I need:

SELECT * FROM `Example`
WHERE ('$startdate' BETWEEN Date1 AND Date2) 
    OR ('$enddate' BETWEEN Date1 AND Date2)
    OR (Date1 BETWEEN '$startdate' AND '$enddate') 
    OR (Date2 BETWEEN '$startdate' AND '$enddate') 
    AND Answered = 1
DST
  • 49
  • 1
  • 8
0

If I'm understanding correctly you'd want:

SELECT * FROM Example 
WHERE (Date1 BETWEEN '$startdate' AND '$enddate') 
    AND (Date2 BETWEEN '$startdate' AND '$enddate') 
    AND Answered = 1
Samuel Cook
  • 16,620
  • 7
  • 50
  • 62
0

If you want to include the start and end points, what about:

SELECT * 
FROM `Example` 
WHERE `Answered` = 1
AND `Date1` >= '$startdate'
AND `Date2` <= '$enddate'

If you do not want to include them in the results:

SELECT * 
FROM `Example` 
WHERE `Answered` = 1
AND `Date1` > '$startdate'
AND `Date2` < '$enddate'

You should probably read here too: PHP: Prepared Statements - Manual

Craig van Tonder
  • 7,497
  • 18
  • 64
  • 109
  • Thats not that what I am searching for, I want to know if any Day from the timespan between the startdate and the enddate is inside the timespan between Date1 and Date2 – DST Mar 30 '16 at 21:30
0

Below query will give you exact data between two dates with answered 1

SELECT * FROM `Example`
WHERE ( `Date1` <= '$startdate' and `Date2` >= '$startdate' )
OR ( '$startdate' <= `Date1`  and '$enddate' >= `Date1` )
OR ( `Date1` < '$enddate' and `Date2` >= '$enddate' )
AND `Aswered` = 1