我有一个数据库,其中包含有关日常功能开放时间的信息。
通常,数据库只有函数ID和开放时间,但这里我以函数名为例。
Table: opening_times
| function_id | function_name | day_open | day_close | eve_open | eve_close |
|-------------|------------------|------------|------------|------------|------------|
| 1 | Charity Function | 1357027200 | 1357056000 | 1357063200 | 1357081200 |
我想编写一个SQL语句,查看该函数是否具有:
这显然必须将当前时间[戳记]与我存储在数据库中的两个日期范围进行比较。
有人可以帮助我实现这一目标。一旦我掌握了语法,我就会学习!
我可以做一个标准的IF语句,看看它是否在 ONE 时间范围之间,但这显然无法达到我需要的结果:
谢谢!
答案 0 :(得分:0)
试试这个::
SELECT
CASE IF UNIX_TIMESTAMP() BETWEEN ot.`day_open` AND ot.`day_close`
THEN 'Within daytime open range'
ELSE 'Not in daytime open range' END as open_status
FROM opening_times ot
答案 1 :(得分:0)
SELECT
CASE
WHEN UNIX_TIMESTAMP( ) < ot.`day_open`
THEN 'Function has not started'
WHEN UNIX_TIMESTAMP() BETWEEN ot.`day_open` AND ot.`day_close`
THEN 'function open'
WHEN UNIX_TIMESTAMP() BETWEEN ot.`day_close` AND ot.`eve_open`
THEN 'Mid-Day Break'
WHEN UNIX_TIMESTAMP() BETWEEN ot.`eve_open` AND ot.`eve_close`
THEN 'Evening Open'
WHEN UNIX_TIMESTAMP() > ot.`eve_close`
THEN 'Closed for day'
END AS function_open_status
FROM opening_times ot WHERE ot.function_id=1