我在编写mysql选择查询时遇到了问题,在这个查询中,我可以获得时隙中的空闲座位数。
让我定义我的问题用户mysql查询:
假设我有一个桌子restaurant_booking。

如果餐厅总共有25张座位,而有一位用户想在下午2点至4点之间使用15座位,那么如何在预订表中登记到这一时间为止,还有多少座位可用?
如果我看到这张订位表,到现在为止已经订好了20的座位,那我怎么才能得到5个座位呢?
如果有人知道,请告诉我。谢谢。
发布于 2013-07-10 10:09:28
逻辑看起来像是:
SELECT (25-SUM(booked_seat)) FROM rest_booking
WHERE end_booking_time>2
AND
start_booking_time<4
--2 & 4 are the new customer's start&end booking time但我同意@Jesbus,日期应该作为时间戳存储。
发布于 2013-07-10 10:11:51
试一试
select
sum(booked_seat) as seatsAva
from
restaurant_booking
where
STR_TO_DATE(end_booking_time,'%r') < DATE_FORMAT(NOW(), '%r')在此之后,您可以减去seatsAva从25,上面的query将给您的结果,目前unavailable席位计数
发布于 2013-07-10 10:42:52
这应该可以做到这一点(假设您的列是日期或unix时间戳):
SELECT (25-SUM(booked_seat)) AS free_seats
FROM restaurant_booking
WHERE
(end_booking_time>start_time_of_desired_booking AND end_booking_time<=end_time_of_desired_booking)
OR
(start_booking_time>=start_time_of_desired_booking AND
start_booking_time<end_time_of_desired_booking)用您的值替换start_time_of_desired_booking和end_time_of_desired_booking,让我知道它是否适合您
https://stackoverflow.com/questions/17567620
复制相似问题