Jika Anda tidak dapat mengubah tabel, Anda dapat menggunakan kueri ini:
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
Jika Anda dapat memodifikasi tabel agar terlihat seperti ini:
+---------+----------+-----------+
| 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 |
+---------+----------+-----------+
maka Anda dapat melakukan kueri seperti ini:
SELECT COUNT(*)
FROM opening_hrs
WHERE (100*HOUR(NOW())+MINUTE(NOW()) >= openTime)
AND (100*HOUR(NOW())+MINUTE(NOW()) <= closeTime)
AND day = 1
Perhatikan bahwa tidak ada lagi openDay dan closeDay; hanya day
kolom. Perhatikan juga bahwa openTime
selalu kurang dari closeTime
, sehingga ketidaksetaraan dalam kueri dapat berfungsi.