Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to check if a date is a specific day in the week in MySQL?

I have a mysql table presence with a row

  • id: 1
  • uid: 14
  • start: 2021-11-01 00:00:00
  • end: 2021-12-31 00:00:00
  • repetitive: 1

This row gives me the information, that the user 14 is present every Monday from 2021-11-01 to 2021-12-31.

SELECT 1 FROM presences
WHERE 2021-11-15 BETWEEN DATE(`start`) AND DATE(`end`)

checks if the given date (2021-11-15) is between his presence dates but how can I add logic to check for "every Monday"? Additionally, if end is null, it should check for every Monday in future without end.
Monday is given by start date and can be every other day as well.
repetitive gives me to check that given day by start. If repetitive is 0, it should just check like my query already does, if the date is between start and end.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Human query:
Get all rows from presence where given date is between start and end, if end is not null and where given day is day of start.

>Solution :

Get all rows from presence where given date is between start and end,
if end is not null and where given day is day of start.

could be humanly translated to:

SELECT *
FROM t
WHERE '2021-11-15' >= `start`
AND (
    '2021-11-15' <= `end` OR
    `end` IS NULL
)
AND (
    repetitive = 0 OR
    repetitive = 1 AND WEEKDAY('2021-11-15') = WEEKDAY(`start`)
)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading