Tuesday, March 6, 2018

MySQL: SELECT * FROM `table_name` WHERE start and end date within two dates

My query should include the following:
  1. Everything that starts between 2018-03-04 and 2018-03-10
  2. Everything that is due between 2018-03-04 and 2018-03-10
  3. Everything that the period of start_task and due_date falls within 2018-03-04 and 2018-03-10
Answer:

      So you are just missing anything where the start - due task date period spans the week you are checking? You should try this:
SELECT 
    * 
FROM 
    `tasks` tasks 
WHERE 
    (
        (`start_task` >= '2018-03-05 00:00:00' and `start_task` <= '2018-03-11 23:59:59') 
        OR (`due_date` >= '2018-03-05 00:00:00' and `due_date` <= '2018-03-11 23:59:59')
        OR (`start_task` <= '2018-03-11 23:59:59' and `due_date` >= '2018-03-05 00:00:00')
    )

No comments:

Post a Comment

MySQL: SELECT * FROM `table_name` WHERE start and end date within two dates

My query should include the following: Everything that starts between 2018-03-04 and 2018-03-10 Everything that is due between 2018-03-0...