我在mysql中有两个表(shop_details,预订)
shop_details
id shop_id open_time close_time
1 1 09:00Am 09:00Pm
2 5 10:00Am 09:00Pm
预订
id shop_id start_time end_time
1 1 10:30am 11:10Am
1 5 02:00pm 02:45pm
现在,我想了解一下shop_id(1),那么我想每半小时获取预订时间(开店时间之后)和可利用的时间(直到商店关闭),例如,我想要跟踪结果>
shop_id start_time end_time status
1 09:00am 09:30am avaliable
1 09:30am 10:00am avaliable
1 10:00am 10:30am avaliable
1 10:30am 11:10am booked
1 11:10am 11:40am avaliable
...
1 08:30am 09:00pm booked
答案 0 :(得分:0)
您需要一个时间列表-然后您可以使用join
或相关子查询来获取信息:
select t.tme,
(case when exists (select 1
from booking b
where b.start_time <= t.tme and
b.end_time > t.tme
)
then 'booked' else 'available'
end) as status
from (select cast('00:00' as time) as tme union all
select cast('00:30' as time) as t union all
select cast('01:00' as time) as t union all
. . .
select cast('23:50' as time) as t
) t join
(select sd.*
from shop_details sd
where sd.shop_id = 1
) sd1
on t.tme >= sd1.open_time and
t.tme <= sd1.close_time;
答案 1 :(得分:-1)
首先,使用while函数创建一个循环,该循环将重复执行代码直到达到关闭时间为止, 在while函数中,您可以使用IF语句根据日期确定可用性状态
好运