D
D
Dymok2021-09-18 09:22:55
MySQL
Dymok, 2021-09-18 09:22:55

How to write such SQL query with date comparison?

There are records in the construction site database that have work_start_date and word_end_date columns that store the start and end dates of work on the site. You need to make a filter by date range so that, for example, by the filter range 09/15/2020 - 09/20/2020, all objects that were worked on on the dates specified in the filter are displayed, even if the work_start_date field in the table row is less than the first filter value and the field work_end_date is greater than the second filter value.

That is, the filter 09/15/2020 - 09/20/2020 should display objects with work dates:
09/14/2020 - 09/18/2020
09/16/2020 - 09/18/2020
09/18/2020 - 09/21/2020

Is it possible to do this? Please tell me where to look, I didn't find anything myself

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2021-09-18
@Rsa97

First you need to convert the date storage to the standard DATE, 2020-09-15 format.
After that, the condition becomes trivial

WHERE `work_start_date` <= :searchEndDate
  && `work_end_date` >= :searchStartDate

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question