位于打开和关闭时间之间的测试,TIME(NOW())
并将其布尔结果作为列返回。如果您OPEN,CLOSE
是 MySQL DATETIME 或 TIME 列,这将按原样工作。
SELECT
ID,
OPEN,
CLOSE,
CASE WHEN TIME(NOW()) BETWEEN `OPEN` AND `CLOSE` THEN 1 ELSE 0 END AS PLACE_IS_OPEN
FROM yourtable
如果这些是字符列而不是正确的TIME
类型,则需要STR_TO_TIME()
转换它们
CASE WHEN TIME(NOW()) BETWEEN TIME(STR_TO_DATE(`OPEN`, '%H:%i')) AND TIME(STR_TO_DATE(`CLOSE`, '%H:%i')) THEN 1 ELSE 0 END AS PLACE_IS_OPEN
MySQL 应该允许你简化CASE
语句,因为它BETWEEN
会返回 0 或 1:
SELECT
ID,
OPEN,
CLOSE,
(TIME(NOW()) BETWEEN TIME(STR_TO_DATE(`OPEN`, '%H:%i')) AND TIME(STR_TO_DATE(`CLOSE`, '%H:%i'))) AS PLACE_IS_OPEN
FROM yourtable