paulomartinhago
4/14/2014 - 6:06 PM

SELECT entre 2 horários utilizando CAST no MySQL (Ex.: registros entre 22:00:00 e 05:00:00)

SELECT entre 2 horários utilizando CAST no MySQL (Ex.: registros entre 22:00:00 e 05:00:00)

SET @time = '03:15:00';

SELECT *
FROM `table`
WHERE IF(
        `start` < `end`,
        CAST(@time AS TIME) BETWEEN `start` AND `end`,
        (CAST(@time AS TIME) BETWEEN CAST('00:00:00' AS TIME) AND `end`) OR 
        (CAST(@time AS TIME) BETWEEN `start` AND CAST('24:00:00' AS TIME))
    ) = 1;