Pokud nemůžete upravit tabulku, můžete použít tento dotaz:
SELECT COUNT(*)
FROM opening_hrs
WHERE (((100*HOUR(NOW())+MINUTE(NOW()) >= openTime)
AND (100*HOUR(NOW())+MINUTE(NOW()) <= closeTime + 2400*(closeDay-openDay)))
OR
((100*HOUR(NOW())+MINUTE(NOW()) >= openTime - 2400*(closeDay-openDay))
AND (100*HOUR(NOW())+MINUTE(NOW()) <= closeTime )))
AND openDay = 1
Pokud můžete upravit tabulku, aby vypadala takto:
+---------+----------+-----------+
| day | openTime | closeTime |
+---------+----------+-----------+
| 0 | 1800 | 2400 |
| 1 | 1800 | 2400 |
| 2 | 1800 | 2400 |
| 3 | 1800 | 2400 |
| 4 | 1800 | 2400 |
| 5 | 1800 | 2400 |
| 6 | 1800 | 2400 |
| 0 | 0 | 400 |
| 1 | 0 | 400 |
| 2 | 0 | 400 |
| 3 | 0 | 400 |
| 4 | 0 | 400 |
| 5 | 0 | 400 |
| 6 | 0 | 400 |
+---------+----------+-----------+
pak byste mohli provést dotaz takto:
SELECT COUNT(*)
FROM opening_hrs
WHERE (100*HOUR(NOW())+MINUTE(NOW()) >= openTime)
AND (100*HOUR(NOW())+MINUTE(NOW()) <= closeTime)
AND day = 1
Všimněte si, že již neexistuje openDay a closeDay; jen day
Všimněte si také, že openTime
je vždy menší než closeTime
, takže nerovnosti v dotazu mohou fungovat.