我在餐厅餐厅餐厅开放和放大工作日休息时间(周一至周日)。我需要使用open& amp;来查找餐厅状态。关闭时间。此外,我还要列出第一家开业餐厅,然后关闭餐厅。
restaurant table
----------------
resid resname
1 Res1
2 Res2
3 Res3
4 Res4
5 Res5
6 Res6
7 Res7
restaurant_time
----------------
resid mon_otime mon_ctime tue_otime tue_ctime wed_otime wed_ctime thu_opentime
1 06:00 23.30 06:00 23.30 06:00 23.30 10:00
2 06:00 23.30 06:00 23.30 06:00 23.30 10:00
3 06:00 23.30 06:00 23.30 06:00 23.30 10:00
4 06:00 23.30 06:00 23.30 06:00 23.30 10:00
5 06:00 23.30 06:00 23.30 06:00 23.30 10:00
6 06:00 23.30 06:00 23.30 06:00 23.30 10:00
7 10:00 23.30 06:00 23.30 06:00 23.30 10:00
我想列出如下的输出......
resid resname Restaurant_status
1 Res1 open
3 Res3 open
4 Res4 open
7 Res7 open
2 Res2 closed
5 Res5 closed
6 Res6 closed
请帮助我,提前致谢。
答案 0 :(得分:0)
首先,如果我正在处理这个问题,我会将restaurant_time
标准化:
CREATE TABLE restaurant_time
(
resid INT,
dayofweek INT,
opentime TIME,
closetime TIME,
PRIMARY KEY (resid, dayofweek),
FOREIGN KEY (resid) REFERENCES restaurant (resid)
) ENGINE = InnoDB
然后我创建一个函数来确定给定餐厅是否在给定时间开放:
CREATE FUNCTION is_open(rest_id INT, theday INT, thetime TIME)
RETURNS BOOLEAN
BEGIN
DECLARE otime TIME;
DECLARE ctime TIME;
SELECT opentime, closetime
INTO otime, ctime
FROM restaurant_times t
WHERE t.resid = rest_id
AND t.dayofweek = theday;
IF (TIMEDIFF(opentime, thetime) < 0)
AND (TIMEDIFF(thetime, closetime) < 0) THEN
RETURN TRUE;
ELSE
RETURN FALSE;
END;
最后,查询
SELECT r.resid, r.name,
IF(is_open(r.resid, DAYOFWEEK(NOW()), TIME(NOW())), "open", "closed")
FROM restaurant r;
应该给你想要的结果。
注意前面的代码没有经过任何方式的测试,也绝不保证没有语法错误。