我知道有关重复发生的事件的问题很常见,但我无法找到一个能够回答这个特定问题的问题,而不是那些与日历应用程序有关的重复事件.主要区别在于我们的应用程序中的事件.只会在报告中或通过他们自己而不是以日历格式出现,尽管在许多方面它们非常相似,可能只是与日历相关的行李较少.
与日历应用程序类似.事件可以一次性发生,也可以反复发生.每个星期四或每个月的第一个星期一,直到将来某个预先设定的时间.
事件存储在事件表中,该事件表包含开始和结束日期以及“重新加密类型ID”.如果’reurrency type’为’None’,则开始日期和结束日期将相同.事件表将ID保存到一个单独的表中,该表保存事件类型名称,例如. ‘会议’或’每周报告’
还有一个表包含“重复货币类型”列表. ‘不再发生’,’每个星期一’,’月的第一个星期一’和’月的最后一个星期六’.
为了使查找更容易,另一个表包含1960年至2060年的日期列表以及有关每个日期的相关信息,例如是否是星期一,以及星期一的星期几.
这允许查找如下:
SELECT disTINCT(e.eventID),n.nameID,n.firstname,n.lastname,d.dt,r.recurringFROM dates d left JOIN recurringtypes r/* if event recurring every week E.g. 'Every Monday' */ON (r.rectypeID BETWEEN 2 AND 8 AND r.day = d.dow) /* if event recurring every month E.g. 'First Monday,every month' */OR ((r.rectypeID BETWEEN 9 AND 36) AND r.day = d.dow AND r.occurrence = d.occurrence) /* if event recurring every last week of month E.g. 'Last Monday,every month' */OR (r.rectypeID >= 37 AND r.day = d.dow and r.islast = d.islast)left JOIN events e on e.rectypeID = r.rectypeIDleft JOIN eventtypes t ON e.eventtypeID = t.eventtypeIDleft JOIN names n ON e.namesID = n.namesIDWHERE (d.dt BETWEEN '2012/02/01' AND '2012/05/01')ORDER BY d.dt;
这正是查找重复事件所需的内容,输出如下:
+-----------+---------------+-------------------+-----------+------------+-------------------------------+| eventID | nameID | lastname | firstname | dt | recurring |+-----------+---------------+-------------------+-----------+------------+-------------------------------+| 3291788 | 1728449 | smith | zoe | 2012-02-02 | First Thursday,every month || 3291797 | 1765432 | | | 2012-02-05 | First Sunday,every month || 3291798 | 1730147 | | | 2012-02-05 | First Sunday,every month || 3291803 | 1790061 | Carpenter | RichIE | 2012-02-06 | Every Monday || 3291805 | 1790061 | Carpenter | RichIE | 2012-02-08 | Second Wednesday,every month || 3291803 | 1790061 | Carpenter | RichIE | 2012-02-13 | Every Monday || 3291799 | 1790061 | Carpenter | RichIE | 2012-02-15 | Third Wednesday,every month || 3291803 | 1790061 | Carpenter | RichIE | 2012-02-20 | Every Monday |
要获得无重复事件,可以使用更简单的查询:
SELECT n.nameID,e.firstdate,e.eventID,'No' as RecurringFROM events eleft JOIN names n ON n.names = e.namesID AND e.rectypeID <= 1 AND e.firstdate BETWEEN '2012/02/01' AND '2012/05/01' AND e.eventID IS NOT NulL ORDER BY e.firstdate;
这使得输出与第一个查询非常相似,但关键的是,日期来自事件表而不是日期表.
我的问题是:我如何结合这些查询来提出一个包含所有事件的列表,包括日期顺序中的重复和非重复?
这些是表和缩短的选择,为简洁起见,删除了一些列和所有索引:).出于同样的原因,未包括“名称”表.
CREATE table events (eventID int(11) NOT NulL auto_INCREMENT,eventtypeID int(11) DEFAulT '0',firstdate date DEFAulT '1960-01-01' COMMENT 'First event',lastdate date DEFAulT '1960-01-01' COMMENT 'Last event',rectypeID int(11) DEFAulT '1');+---------+-------------+------------+------------+-----------+| eventID | eventtypeID | firstdate | lastdate | rectypeID |+---------+-------------+------------+------------+-----------+| 3291803 | 16 | 2012-02-03 | 2012-04-11 | 3 || 3291797 | 8 | 2012-02-12 | 2012-02-22 | 9 || 3291798 | 5 | 2012-02-12 | 2012-02-12 | 9 || 3291788 | 8 | 2012-05-24 | 2015-01-16 | 13 || 3291805 | 10 | 2012-01-04 | 2012-02-14 | 19 || 3291799 | 16 | 2012-02-07 | 2012-10-24 | 26 || 3291804 | 5 | 2012-02-03 | 2012-08-22 | 41 |+---------+-------------+------------+------------+-----------+CREATE table CMSeventtypes (eventtypeID int(11) NOT NulL auto_INCREMENT,eventtype varchar(50) DEFAulT '' COMMENT 'Event type AKA name');+-------------+----------------------+| eventtypeID | eventype | +-------------+----------------------+| 1 | Follow up meeting || 2 | Reminder email due || 3 | Monthly meeting || 4 | Weekly report || 5 | Golf practice |+------------------------------------+CREATE table recurringtypes (rectypeID int(11) NOT NulL auto_INCREMENT,recurring varchar(40) DEFAulT '',day tinyint(4) DEFAulT '0',occurrence tinyint(4) DEFAulT '0',islast tinyint(4) DEFAulT '0');+-----------+---------------------------+------+------------+--------+| rectypeID | recurring | day | occurrence | islast |+-----------+---------------------------+------+------------+--------+| 1 | No | 0 | 0 | 0 || 2 | Every Sunday | 1 | 0 | 0 || 3 | Every Monday | 2 | 0 | 0 || 4 | Every Tuesday | 3 | 0 | 0 || 5 | Every Wednesday | 4 | 0 | 0 || 6 | Every Thursday | 5 | 0 | 0 || 7 | Every FrIDay | 6 | 0 | 0 || 8 | Every Saturday | 7 | 0 | 0 || 9 | First Sunday,every month | 1 | 1 | 0 || 10 | First Monday,every month | 2 | 1 | 0 |+-----------+---------------------------+------+------------+--------+CREATE table dates (dt date NOT NulL COMMENT 'Date',daycount mediumint(9) NOT NulL DEFAulT '1',year smallint(6) NOT NulL DEFAulT '1970',month tinyint(4) NOT NulL DEFAulT '1',dom tinyint(4) NOT NulL DEFAulT '1',dow tinyint(4) NOT NulL DEFAulT '1',occurrence tinyint(4) NOT NulL DEFAulT '0',islast tinyint(1) NOT NulL DEFAulT '0');+------------+----------+------+-------+-----+-----+------------+--------+| dt | daycount | year | month | dom | dow | occurrence | islast |+------------+----------+------+-------+-----+-----+------------+--------+| 2012-02-02 | 734900 | 2012 | 2 | 2 | 5 | 1 | 0 || 2012-02-03 | 734901 | 2012 | 2 | 3 | 6 | 1 | 0 || 2012-02-04 | 734902 | 2012 | 2 | 4 | 7 | 1 | 0 || 2012-02-05 | 734903 | 2012 | 2 | 5 | 1 | 1 | 0 || 2012-02-06 | 734904 | 2012 | 2 | 6 | 2 | 1 | 0 || 2012-02-07 | 734905 | 2012 | 2 | 7 | 3 | 1 | 0 || 2012-02-08 | 734906 | 2012 | 2 | 8 | 4 | 2 | 0 || 2012-02-09 | 734907 | 2012 | 2 | 9 | 5 | 2 | 0 |+------------+----------+------+-------+-----+-----+------------+--------+
我们并非完全使用上面的代码或表格布局,欢迎任何有效的解决方案.请不要指向我:
How would you store possibly recurring times?
What’s the best way to model recurring events in a calendar application?
Should I store dates or recurrence rules in my database when building a calendar app?
要么
http://tools.ietf.org/html/rfc5545
我已经检查了它们,它们非常有用,但没有像我们想要的那样做.
TIA
最佳答案SELECT disTINCT(e.eventID),every month' */OR (r.rectypeID >= 37 AND r.day = d.dow and r.islast = d.islast)left JOIN events e on e.rectypeID = r.rectypeID OR (e.rectypeID <= 1 AND e.eventID IS NOT NulL) left JOIN eventtypes t ON e.eventtypeID = t.eventtypeIDleft JOIN names n ON e.namesID = n.namesIDWHERE (d.dt BETWEEN '2012/02/01' AND '2012/05/01')ORDER BY d.dt;
总结 以上是内存溢出为你收集整理的mysql – 重复事件,SQL查询全部内容,希望文章能够帮你解决mysql – 重复事件,SQL查询所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)