Here is a query I wrote up to give you the most recent thursday and the ending wednesday
SELECT thuwk_beg + INTERVAL 0 second thu_beg,
thuwk_beg + INTERVAL 604799 second wed_end
FROM (SELECT (DATE(NOW()) - INTERVAL daysbacktothursday DAY) thuwk_beg
FROM (SELECT SUBSTR('3456012',wkndx,1) daysbacktothursday
FROM (SELECT DAYOFWEEK(dt) wkndx FROM (SELECT DATE(NOW()) dt) AAAA) AAA) AA) A;
Here is an example for today, 2011-09-21
mysql> SELECT
-> thuwk_beg + INTERVAL 0 second thu_beg,
-> thuwk_beg + INTERVAL 604799 second wed_end
-> FROM (SELECT (DATE(NOW()) - INTERVAL daysbacktothursday DAY) thuwk_beg
-> FROM (SELECT SUBSTR('3456012',wkndx,1) daysbacktothursday
-> FROM (SELECT DAYOFWEEK(dt) wkndx FROM (SELECT DATE(NOW()) dt) AAAA) AAA) AA) A;
+---------------------+---------------------+
| thu_beg | wed_end |
+---------------------+---------------------+
| 2011-09-15 00:00:00 | 2011-09-21 23:59:59 |
+---------------------+---------------------+
1 row in set (0.00 sec)
Just replace the NOW() function calls with whatever datetime you like and you will have the week starting Thursday all the time for the give datetime you choose.
Here is another example using the specific date '2011-01-01'
mysql> SELECT
-> thuwk_beg + INTERVAL 0 second thu_beg,
-> thuwk_beg + INTERVAL 604799 second wed_end
-> FROM (SELECT (DATE('2011-01-01') - INTERVAL daysbacktothursday DAY) thuwk_beg
-> FROM (SELECT SUBSTR('3456012',wkndx,1) daysbacktothursday
-> FROM (SELECT DAYOFWEEK(dt) wkndx FROM (SELECT DATE('2011-01-01') dt) AAAA) AAA) AA) A;
+---------------------+---------------------+
| thu_beg | wed_end |
+---------------------+---------------------+
| 2010-12-30 00:00:00 | 2011-01-05 23:59:59 |
+---------------------+---------------------+
1 row in set (0.00 sec)
Your query of table
referencing today would resemble something like this:
SELECT * from `table`,
(SELECT thuwk_beg + INTERVAL 0 second thu_beg,
thuwk_beg + INTERVAL 604799 second wed_end
FROM (SELECT (DATE(NOW()) - INTERVAL daysbacktothursday DAY) thuwk_beg
FROM (SELECT SUBSTR('3456012',wkndx,1) daysbacktothursday
FROM (SELECT DAYOFWEEK(dt) wkndx FROM (SELECT DATE(NOW()) dt) AAAA) AAA) AA) A) M
WHERE `date` >= thu_beg
AND `date` <= wed_end;
Give it a Try !!!
UPDATE 2011-09-22 16:27 EDT
This was my proposed query for marking Thu-Wed.
SELECT thuwk_beg + INTERVAL 0 second thu_beg,
thuwk_beg + INTERVAL 604799 second wed_end
FROM (SELECT (DATE(NOW()) - INTERVAL daysbacktothursday DAY) thuwk_beg
FROM (SELECT SUBSTR('3456012',wkndx,1) daysbacktothursday
FROM (SELECT DAYOFWEEK(dt) wkndx FROM (SELECT DATE(NOW()) dt) AAAA) AAA) AA) A;
How about other weeks ???
(SELECT SUBSTR('6012345',wkndx,1)
does the week starting Mon ending Sun
(SELECT SUBSTR('5601234',wkndx,1)
does the week starting Tue ending Mon
(SELECT SUBSTR('4560123',wkndx,1)
does the week starting Wed ending Tue
(SELECT SUBSTR('3456012',wkndx,1)
does the week starting Thu ending Wed
(SELECT SUBSTR('2345601',wkndx,1)
does the week starting Fri ending Thu
(SELECT SUBSTR('1234560',wkndx,1)
does the week starting Sat ending Fri
(SELECT SUBSTR('0123456',wkndx,1)
does the week starting Sun ending Sat
If the concern is manual maintenance of a large number of rows, you could solve this (potentially) by creating tables for each of your dimensions: weeks, time slots, and venues. The complete set of possible slots for booking would be the cross product of these three dimensions. Actual bookings would be another table with foreign keys pointing to all three of these dimension tables.
With this type of design, instead of maintaining 3 x 25 x 26 records you will maintain 3 + 25 + 26 records. Note that if you segregate day of week and time of day into two tables you can reduce the number of records to be maintained even further (3 + 5 + 5 + 26).
The problem with this approach is when (and if) you have an exception. This design assumes that there are no blackouts in your schedule. For example, what if you close a room for a couple of weeks to be renovated? One way to handle this issue is to create booking records that cover the blackouts. If you have enough exceptions, then managing them may be almost as bad as just using the brute force method.
The question I would seriously consider is whether or not generating the initial list of slots available for booking is really that big a deal. You could easily automate the process to generate your big pile of available slots. This is really just a single query.
Best Answer
IMO you overcomplicate things. Just add an
available DATE
field to thecandidate
table. Then queries will be simpler and more performant.