Hvis du ikke kan ændre tabellen, kan du bruge denne forespørgsel:
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
Hvis du kan ændre tabellen til at se sådan ud:
+---------+----------+-----------+
| 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 |
+---------+----------+-----------+
så kan du udføre forespørgslen sådan her:
SELECT COUNT(*)
FROM opening_hrs
WHERE (100*HOUR(NOW())+MINUTE(NOW()) >= openTime)
AND (100*HOUR(NOW())+MINUTE(NOW()) <= closeTime)
AND day = 1
Bemærk, at der ikke er mere openDay og closeDay; bare en day
kolonne. Bemærk også, at openTime
er altid mindre end closeTime
, så ulighederne i forespørgslen kan fungere.