Php 在sql中获取可用时间和预定时间
我在mysql商店有两张桌子,详细信息,预订 店铺详情Php 在sql中获取可用时间和预定时间,php,mysql,sql,Php,Mysql,Sql,我在mysql商店有两张桌子,详细信息,预订 店铺详情 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
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
现在我想,如果我想知道店铺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
首先,使用while函数创建一个循环,该循环将重复代码直到到达关闭时间, 在while函数中,您可以使用IF语句使可用性状态基于日期
goold luck您需要一个时间列表-然后您可以使用连接或相关子查询获取信息:
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;
展示你以前实现它的尝试,你的代码,等等。记住,这不是一支编码大军,这是一个帮助你的地方,没有一个为你工作的地方。为什么有时会有45分钟的预订,有时是40分钟的预订。如果有至少30分钟的未预订时间,您想显示可用,而只显示这30分钟?如果50是空闲时间,为什么不是50呢?你的数据很奇怪。id和店铺id的区别是什么?预期的最后一行来自何处?请参阅:我们可以知道开始时间和结束时间的数据类型是什么吗?还有,如果我们预定15分钟,剩下的怎么办?