简体   繁体   English

存储多个日期范围

[英]Storing multiple date ranges

So I'm developing a carpooling system whereby users can submit what dates they can offer a lift for, or what time they are wanting to get lifts. 因此,我正在开发一个拼车系统,用户可以在该系统中提交可以提供乘车服务的日期,或者他们希望在什么时间乘车。

The form itself is going to be using a virtualised calendar where users can pick dates by clicking on them or select ranges of dates by shift-clicking for this. 表单本身将使用虚拟日历,用户可以通过单击日历来选择日期,也可以通过按住Shift键并单击来选择日期范围。 They will also be able to pick multiple types of selection, IE they can use a range of dates then include one or two more dates that don't obviously come in that range. 他们还将能够选择多种类型的选择,即IE他们可以使用日期范围,然后再添加一个或两个显然不在该范围内的日期。

The format that will be produced for these dates will be as follows: 这些日期将产生的格式如下:

Standard one date: yyyy-mm-dd 标准日期:yyyy-mm-dd

Multiple dates: 多个日期:

yyyy-mm-dd, yyyy-mm-dd yyyy-mm-dd,yyyy-mm-dd

One range of dates: 日期范围:

yyyy-mm-dd - yyyy-mm-dd yyyy-mm-dd-yyyy-mm-dd

Multiple ranges of dates: 多个日期范围:

yyyy-mm-dd - yyyy-mm-dd, yyyy-mm-dd - yyyy-mm-dd yyyy-mm-dd-yyyy-mm-dd,yyyy-mm-dd-yyyy-mm-dd

Mix: 混合:

yyyy-mm-dd - yyyy-mm-dd, yyyy-mm-dd yyyy-mm-dd-yyyy-mm-dd,yyyy-mm-dd

Now I'm curious about the best way of storing this into a database. 现在,我对将其存储到数据库中的最佳方式感到好奇。 Is it possible to store all of this information into one array of dates of some kind / another table. 是否可以将所有这些信息存储到某种日期/另一表的日期数组中。 Or should it create multiple instances for each range? 还是应该为每个范围创建多个实例?

Probably better if I show you what I had in mind for the tables: 如果我告诉您这些表格的想法,可能会更好:

If a user (id = 1) wants to offer lifts for 2014-04-01 - 2014-04-06 and also 2014-04-09 - 2014-04-11, the database at the moment will do the following: 如果用户(id = 1)想要提供2014-04-01-2014-04-06以及2014-04-09-2014-04-11的电梯,此刻数据库将执行以下操作:

  id  userid startDate  EndDate
 ---- ------ ---------- ----------
    1     1  2014-04-01 2014-04-06
    2     1  2014-04-09 2014-04-11

Or should it be something using multiple tables or as an array of many individual dates as opposed to a range? 还是应该使用多个表或将多个单独的日期(而不是范围)作为一个数组?

The reason that it matters is because when someone is searching for someone who has offered lifts, they obviously want to see all of the available lifts from that person sometimes, therefore, should it be displayed as one record if the users submits it as one "offer". 之所以重要,是因为当某人搜索提供升降机的人时,他们显然显然有时希望查看该人提供的所有可用升降机,因此,如果用户将其提交为“提供”。

Dates will be produced by json and inserted into database via php->pdo. 日期将由json生成,并通过php-> pdo插入数据库。 Database is mysql. 数据库是mysql。

Also note that the table for the request will hold more data than is displayed, but that's only what's necessary here. 还要注意,请求表将容纳比显示更多的数据,但这仅是这里所需要的。 Main issue is I don't want repeated data everywhere for multiple ranges of dates. 主要问题是我不想在多个日期范围内到处重复数据。

I'm sorry if I haven't explained this well enough, it's quite complicated to get the idea across. 很抱歉,如果我还没有很好地解释这一点,那么将其传达出来是相当复杂的。

Thanks for your time. 谢谢你的时间。 Andy. 安迪。

I can't remember the reason now but when I worked on a trading system, we also had to put the trade positions over time in a similar date structure so that they could be queried easily for reporting purposes. 我现在不记得原因了,但是当我使用交易系统时,我们还必须将一段时间内的交易头寸放置在相似的日期结构中,以便可以出于查询目的方便地查询它们。 We would have a structure like this: 我们将有一个这样的结构:

  id  userid startDate  EndDate
 ---- ------ ---------- ----------
    1     1  2014-04-01 2014-04-06
    2     1  2014-04-06 2014-04-09
    3     1  2014-04-09 2014-04-11
    4     1  2014-04-11 null

I can't remember the exact reason why we did this now but it was something to do with getting all the positional data back quickly and easily. 我不记得我们现在为什么这样做的确切原因,但这与快速,轻松地获取所有位置数据有关。

I agree with IK 我同意IK

To address some of your additional questions: 要解决您的一些其他问题:

Or should it be something using multiple tables or as an array of many individual dates as opposed to a range? 还是应该使用多个表或将多个单独的日期(而不是范围)作为一个数组?

It would be a bad idea to store every date in that range. 将每个日期存储在该范围内将是一个坏主意。 Not only will it fill up your database fast, it requires lots of operations to insert, and is more complex to amend (eg if the user wants to reduce the range). 它不仅会快速填满您的数据库,而且还需要插入很多操作,并且修改起来更加复杂(例如,如果用户想缩小范围)。

Regarding 'single dates'. 关于“单个日期”。 I would still store this as a range. 我仍将其存储为范围。 You could null out the end date, as suggested by IK, but i'd rather just have a single day range, eg 您可以按照IK的建议将结束日期设为空,但是我只希望有一天的时间范围,例如

id  userid startDate  EndDate
--- ------ ---------- --------
4   1      2014-04-11 2014-04-11

From a coding point of view it's more consistent, and means you don't have to deal with 'special cases' in your code when the end date is null. 从编码的角度来看,它更加一致,这意味着当结束日期为null时,您不必在代码中处理“特殊情况”。 It also means the null value can be reserved for 'open ended' ranges, if needs be (which is, of course, a special case!). 这也意味着如果需要的话,可以为“开放式”范围保留空值(当然,这是一种特殊情况!)。

I'm also assuming you're using a proper date type in the database, rather than storing as a string :) 我还假设您在数据库中使用了正确的日期类型,而不是存储为字符串:)

I can't remember the exact reason why we did this now but it was something to do with getting all the positional data back quickly and easily. 我不记得我们现在为什么这样做的确切原因,但这与快速,轻松地获取所有位置数据有关。

Just to elaborate this a little bit further. 为了进一步阐述这一点。 You will gain some performance when you denormalize the table and use the "Date" as Datatype, because you avoided at least two joins. 对表进行非规范化并将“日期”用作数据类型时,将获得一些性能,因为避免了至少两个联接。 But it alway depend on the varoius ciriculumstances, for example if you prefer performance over deduplication and vice versa. 但这始终取决于精打细算的情况,例如,如果您希望性能胜于重复数据删除,反之亦然。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM