I need to get a series of dates using the day of the week and time as the only bit of information.
I am aware of the opposite of this operation, which uses WEEKDAY (date) or DAYOFWEEK (date), but this is not what I need.
I have a table that looks like this: it will be continuously checked and compared with the current date.
+----+---------+----------+----------+ | id | weekday | start | end | +----+---------+----------+----------+ | 1 | 1 | 09:00:00 | 17:00:00 | | 2 | 2 | 09:00:00 | 17:00:00 | | 3 | 3 | 09:00:00 | 17:00:00 | | 4 | 4 | 23:00:00 | 17:00:00 | | 5 | 5 | 18:00:00 | 23:00:00 | | 6 | 6 | 00:00:00 | 00:00:00 | | 7 | 7 | 00:00:00 | 01:00:00 | +----+---------+----------+----------+
There was a simple version above, I expect this table to have hundreds or thousands of possible iterations. As noted in the 4th row, times can also change at midnight, and I must compensate.
I need a query that will provide me with the following result sets, and I am having trouble locking the date based on the current year, month, and past day of the week from the day of the week column.
Example result set:
+---------------------+---------------------+ | start | end | +---------------------+---------------------+ | 2013-06-24 09:00:00 | 2013-06-24 17:00:00 | | 2013-06-25 09:00:00 | 2013-06-25 17:00:00 | | 2013-06-26 09:00:00 | 2013-06-26 17:00:00 | | 2013-06-27 23:00:00 | 2013-06-28 17:00:00 | # <---- Notice the date difference here | 2013-06-28 18:00:00 | 2013-06-28 23:00:00 | | 2013-06-29 00:00:00 | 2013-06-30 00:00:00 | # <---- Also here, the span is 24 hours | 2013-06-30 00:00:00 | 2013-06-30 01:00:00 | +---------------------+---------------------+
Any help would be appreciated, I got as close to the EXACT DATE as possible, but this creates some problems for me, since these dates should be converted later, so I need an array of them, and not just the current day.
Edit: Added my current code: Although I do not believe that this is quite directed in the right direction
SELECT TIMESTAMP(IF (end < start, CURRENT_DATE, SUBDATE(CURRENT_DATE, 1)), start) AS 'start', TIMESTAMP(IF (end < start, CURRENT_DATE, SUBDATE(CURRENT_DATE, 1)) end) AS 'end', FROM shift
And the result:
+---------------------+---------------------+ | start | end | +---------------------+---------------------+ | 2013-06-27 23:00:00 | 2013-06-28 17:00:00 | | 2013-06-28 18:00:00 | 2013-06-28 23:00:00 | # Needs the rest of the dates of the week +---------------------+---------------------+
SECOND EDIT
Thanks to the help of the below commentators, I came to the next request, moving forward, although some of them expressed concern about the design, and I will run some tests before I agree to the answer. If there is a better way to do this, I would be very interested to hear that!
I believe the best moment is that the month changes halfway through the request, using the day of the week to determine what difference can be unpleasant.
Query:
SELECT @diff:=(CAST(weekday AS SIGNED) - (WEEKDAY(CURRENT_DATE) + 1)) as 'diff', @date:=DATE_ADD(CURRENT_DATE, INTERVAL @diff DAY) as 'start_date', TIMESTAMP(@date, start) AS 'start', TIMESTAMP(IF (end <= start, DATE_ADD(@date, INTERVAL 1 DAY), @date), end) AS 'end' FROM shift
Result:
+------+------------+---------------------+---------------------+ | diff | start_date | start | end | +------+------------+---------------------+---------------------+ | -4 | 2013-06-24 | 2013-06-24 09:00:00 | 2013-06-24 17:00:00 | | -3 | 2013-06-25 | 2013-06-25 09:00:00 | 2013-06-25 17:00:00 | | -2 | 2013-06-26 | 2013-06-26 09:00:00 | 2013-06-26 17:00:00 | | -1 | 2013-06-27 | 2013-06-27 23:00:00 | 2013-06-28 17:00:00 | | 0 | 2013-06-28 | 2013-06-28 18:00:00 | 2013-06-28 23:00:00 | | 1 | 2013-06-29 | 2013-06-29 00:00:00 | 2013-06-30 00:00:00 | | 2 | 2013-06-30 | 2013-06-30 00:00:00 | 2013-06-30 10:00:00 | +------+------------+---------------------+---------------------+
Third editor
I tested the above query for periods that span the beginning and end of the month, and found that it was suitable for its purpose. I will translate the above request as well as some notes to the answer in the hope that someone will find this useful in the future.
In this article I will also talk about various errors and recommendations of various commentators, again thanks to everyone who took the time to help.