检查重叠的汽车预订
我正在编写一个简单的汽车租赁预订程序(学校作业)。我和我的伙伴正在努力使系统比任务要求的更先进一些,但我们遇到了一些问题,希望您能帮助我们解决。
这个想法是,您可以预订某种汽车类型,当您获得汽车时,它将是该类型之一(正如我们的任务所规定的,您不预订特定的汽车,而只预订一种类型)。在特定日期只有一位客户可以拥有汽车。随着预订的增加,我们必须确保我们租用的每种类型的汽车不会超过我们现有的数量。预订基本上存储有开始日期、结束日期和汽车类型。
如果我们暂时忽略汽车类型(假设我们只有一种类型),那么预订可能会以图形方式显示如下:
1/12 2/12 3/12 4/12 5/12 6/12 7/12
|-------------------|
|-----------------|
|-----|
|-------|
|-----------|
|-------------|
如果租赁只有三辆车,则可以从 3/12 到 5/12 租一辆车因为全天只有 2 辆车的预订。但我们怎么知道这一点呢?我们是否必须检查每个日期并 count() 跨该日期的预订数量?
如果有人在 4/12 预订了一辆车,那么 3/12 和 5/12 仍然只有 2 个预订,但 4/12 会有 3 个预订。
是否可以通过查询进行一些操作,或者做我们必须遍历程序中的每个日期来检查预订数量是否超过了汽车数量? (只有完整的日期,这很容易,但考虑一下您可以按小时租用汽车的情况(不仅像这里那样每天租用)。然后,如果我们有一个很多预订和汽车,时间跨度很长...)
希望您有一些好的想法可以帮助我们。感谢您花时间阅读问题:)
- Mikkel,丹麦
I'm writing a simple booking program for a car rental (a school assignment). Me and my buddy are trying to make the system a little more advanced than the assignment dictates, but we're having some problems we hoped you could help us with.
The idea is that you can reserve a certain car type, and when you get the car it will be one of that type (you don't reserve a specific car, as our assignment dictates, but only a type). Only one customer can have the car on a specific date. As the reservations tick in we have to make sure, that we don't hire out more cars of each type than we've got. The reservations are basically stored with a start date, an end date, and a car type.
If we ignore the car type for now (lets say we only have one type) then the reservations could graphically look something like this:
1/12 2/12 3/12 4/12 5/12 6/12 7/12
|-------------------|
|-----------------|
|-----|
|-------|
|-----------|
|-------------|
If the rental only has three cars it would be possible to rent a car from 3/12 to 5/12 since all days only have 2 car reservations. But how do we know this? Do we have to check each date and count() the number of reservations that spans over that date?
And what if somebody had reserved a car on 4/12, then 3/12 and 5/12 would still only have 2 reservations, but 4/12 would have 3.
Would it be possible to do with a query some how, or do we have to step through each date in the program to check the number of reservations didn't exceed the number of cars?
(This is easy enough with only full dates, but consider the scenario where you could rent the cars on an hourly basis (not only on a daily as here). Then it could be a though one to step through each our if we have a lot of reservations and cars and the timespan is long...)
Hope you have some nice ideas that will help us along. Thanks for taking the time to read the question :)
- Mikkel, Denmark
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设,您在现实生活中有这样的预订情况:
表
car
表
reservation
期间的所有可用汽车
现在,您必须通过非常简单的逻辑,选择
2013- 12-05 至
2013-12-06
“选择所有没有任何日期预订的汽车,这会阻止其使用”
使用 brillian mysql 选择:
Assume, You have such reservation situation in real life:
Table
car
Table
reservation
Now, You must by really simple logic, select all available cars for period
from
2013-12-05
to2013-12-06
"Select ALL cars, which does not have any reservation with dates, which blocks it for usage"
with brillian mysql select:
“是否可以通过某种方式进行查询,或者我们是否必须逐步检查程序中的每个日期,以检查预订数量没有超过汽车数量?(这很容易,只有完整的日期, “
您的问题的本质是,任何单个日期都可能出现违反约束的情况。因此从逻辑上讲,确实有必要对新预订中包含的每个单独日期进行检查。唯一可能的优化是要执行此操作,您必须首先计算数据库中已出现且与新预订重叠的所有间隔,
例如 4/12-6/ 的新预订 。 12 必须分为 4/12-5/12(第二行)和 5/12-6/12(第三行)。这些单独的间隔可能长于一天,您可以在 上进行检查。这些单独间隔的级别(它们与此特定示例中的单独日期相同,但 7/12-19/12 的预订根本不必分开。
但是,计算这可能会很困难,并且还有另一个警告:当您查看多行插入时,您还应该拆分要插入的其他行(这需要您将所有插入的行记录在临时表中)表,否则您将无法访问它们)。
"Would it be possible to do with a query some how, or do we have to step through each date in the program to check the number of reservations didn't exceed the number of cars? (This is easy enough with only full dates,"
The nature of your problem is that a violation of the constraint could appear on any individual date. So logically speaking, it is indeed necessary to do the check for each individual date comprised in a new reservations. The only optimisation possible would be to do the check at the level of "smallest intervals". To do that, you must first compute all the intervals that already appear in the database, and which overlap with your new reservation.
For example, a new reservation for 4/12-6/12 would have to be split into 4/12-5/12 (second line) and 5/12-6/12 (third line). Those individual intervals might be longer than one single day, and you can do the checks on the level of those individual intervals. (They are the same as individual days in this particular example, but a reservation 7/12-19/12 would not have to be split at all.
However, computing this might prove difficult, and there's another caveat: when you're looking al multi-row inserts, you should also be splitting over the other rows to be inserted (and that requires you to record all the inserted rows in a temporary table, otherwise you won't be able to access them).