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.
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`)
)