Imagine a domotic webapp, to command a heater.
You could setup N plans, for a plan I mean a similar table:
+----+---------+--------------------+
| id | name | target_temperature |
+----+---------+--------------------+
| 1 | confort | 19 |
+----+---------+--------------------+
| 2 | hot | 22 |
+----+---------+--------------------+
| 3 | night | 17 |
+----+---------+--------------------+
PSEUDOCODE
(if plan is confort && temperature<19 then heater on)
Imagine you would setup a similar scenario:
MONDAY:
from 00:00 to 07:00 >> plan is night
from 07:00 to 08:00 >> plan is hot
from 16:00 to 18:00 >> plan is hot
from 18:00 to 22:00 >> plan is confort
from 22:00 to 23:59 >> plan is night
[...]
SATURDAY:
from 00:00 to 23:59 >> plan is confort
Note that on monday from the 08:00 to the 18:00 there is no plan. Software will be setup OFF the heater.
How you would design the database of the days? A singular table with 7 colums (from Monday to Sunday) and every columns N rows? I don't like it, because saturday is totally different from monday.
A singular table with 24 (or 48) rows and 7 column? WHere the rows goes from 00:00 to 23:59
+-----+-------+-------+---------+-----+---------+-------+
| id | hour | M | T | […] | S | S |
+-----+-------+-------+---------+-----+---------+-------+
| 1 | 00:00 | night | hot | | night | night |
+-----+-------+-------+---------+-----+---------+-------+
| 2 | 00:30 | night | night | | confort | null |
+-----+-------+-------+---------+-----+---------+-------+
| 3 | 01:00 | night | night | | confort | null |
+-----+-------+-------+---------+-----+---------+-------+
| 4 | 01:30 | hot | confort | | confort | null |
+-----+-------+-------+---------+-----+---------+-------+
| 5 | 02:00 | hot | hot | | confort | null |
+-----+-------+-------+---------+-----+---------+-------+
| 6 | 02:30 | null | null | | confort | hot |
+-----+-------+-------+---------+-----+---------+-------+
| […] | | | | | | |
+-----+-------+-------+---------+-----+---------+-------+
| 50 | 23:30 | hot | hot | | null | null |
+-----+-------+-------+---------+-----+---------+-------+
| 51 | 23:59 | night | night | | night | null |
+-----+-------+-------+---------+-----+---------+-------+
And where the NULL means "Hey! Put the heater OFF!" ...
So, query to get the CURRENT APPLICABLE PLAN could be a simple
SELECT * FROM TEST WHERE hour =
(
SELECT MAX(hour) FROM TEST WHERE hour < CURTIME()
)
But in this case the difficult could be select the right day! How select only the "Monday" column?
Thank you :)