使用mysql查找打开和关闭状态
我有一张餐厅桌子,每周(周一到周日)都有餐厅的开放和关闭时间。我需要使用开关门时间查找餐厅状态。我还需要列出第一家开业的餐厅,然后是关闭的餐厅使用mysql查找打开和关闭状态,mysql,Mysql,我有一张餐厅桌子,每周(周一到周日)都有餐厅的开放和关闭时间。我需要使用开关门时间查找餐厅状态。我还需要列出第一家开业的餐厅,然后是关闭的餐厅 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
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
请帮助我,提前谢谢。首先,如果我正在解决这个问题,我会将餐厅时间正常化:
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;
应该会给你想要的结果
注意前面的代码没有经过任何测试,也不能保证没有语法错误。首先,如果我要解决这个问题,我会规范化
餐厅时间
:
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;
应该会给你想要的结果
注意前面的代码没有经过任何测试,也不能保证没有语法错误。1。你所说的第一次开业和结束restsurant是什么意思?2.到目前为止你试过什么?3.你应该使这张桌子正常化。对不起,我不知道。所以请解释一下,我是如何编写mysql查询的。首先打开snd关闭restsurant是什么意思?我需要先列出开放状态的餐馆,然后列出关闭状态的餐馆。你所说的第一次开业和结束restsurant是什么意思?2.到目前为止你试过什么?3.你应该使这张桌子正常化。对不起,我不知道。所以请解释一下,我是如何编写mysql查询的。首先打开snd关闭restsurant是什么意思?我需要先列出开放状态的餐馆,然后列出关闭状态的餐馆。