0

I'm trying to return MySQL rows when the start_date and end_date period falls within a date range, either completely or partially.

Here's the SQL I'm using at the moment.

SELECT
        start_date,
        end_date,
        id
    FROM
        employee_leave
    WHERE
        DATE(start_date) >= :start_date_range
    AND DATE(end_date) <= :end_date_range;

This works fine to return rows that fall wholly within the specified range but it needs to also return rows that are partially within the range too.

Example, start_date_range = 2015-07-04 end_date_range = 2015-07-15

If I have the following rows they should all be returned:

  1. start_date = 2015-07-05, end_date = 2015-07-10
  2. start_date = 2015-07-01, end_date = 2015-07-10
  3. start_date = 2015-07-01, end_date = 2015-07-20
  4. start_date = 2015-07-11, end_date = 2015-07-20
tk16
  • 21
  • 2

1 Answers1

0
SELECT
    start_date,
    end_date,
    id
FROM
    employee_leave
WHERE
    (DATE(start_date) >= :start_date_range AND DATE(start_date) <= :end_date_range) 
OR 
    (DATE(end_date) >= :start_date_range AND DATE(end_date) <= :end_date_range);
Alex
  • 8,827
  • 3
  • 42
  • 58