Saya pikir masalahnya ada pada urutan evaluasi di WHERE
. Anda ayat. Saya selalu gunakan tanda kurung di sekitar OR
operator di SQL. Berikut ini berfungsi di MySQL 5.6.
SELECT *
FROM tb_test
WHERE datecreate between '2018-09-24 19:00:00' and '2018-09-26 07:00:00'
AND (hour(DateCreate) >= 19 or hour(DateCreate) < 7);
Untuk referensi, saya menggunakan skema pengujian ini:
CREATE table tb_test
(datecreate datetime);
CREATE OR REPLACE VIEW generator_16
AS SELECT 0 n UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL
SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL
SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL
SELECT 9 UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL
SELECT 12 UNION ALL SELECT 13 UNION ALL SELECT 14 UNION ALL
SELECT 15;
CREATE OR REPLACE VIEW generator_256
AS SELECT ( ( hi.n << 4 ) | lo.n ) AS n
FROM generator_16 lo, generator_16 hi;
INSERT INTO tb_test
SELECT date_add("2018-09-24",interval n hour)
FROM generator_256;