mysql – 重复事件,SQL查询

前端之家收集整理的这篇文章主要介绍了mysql – 重复事件,SQL查询前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我知道有关重复发生的事件的问题很常见,但我无法找到一个能够回答这个特定问题的问题,而不是那些与日历应用程序有关的重复事件.主要区别在于我们的应用程序中的事件.只会在报告中或通过他们自己而不是以日历格式出现,尽管在许多方面它们非常相似,可能只是与日历相关的行李较少.

与日历应用程序类似.事件可以一次性发生,也可以反复发生.每个星期四或每个月的第一个星期一,直到将来某个预先设定的时间.

事件存储在事件表中,该事件表包含开始和结束日期以及“重新加密类型ID”.如果’reurrency type’为’None’,则开始日期和结束日期将相同.事件表将id保存到一个单独的表中,该表保存事件类型名称,例如. ‘会议’或’每周报告’

还有一个表包含“重复货币类型”列表. ‘不再发生’,’每个星期一’,’月的第一个星期一’和’月的最后一个星期六’.

为了使查找更容易,另一个表包含1960年至2060年的日期列表以及有关每个日期的相关信息,例如是否是星期一,以及星期一的星期几.

这允许查找如下:

SELECT DISTINCT(e.eventid),n.nameid,n.firstname,n.lastname,d.dt,r.recurring
FROM 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.rectypeid
LEFT JOIN eventtypes t ON e.eventtypeid = t.eventtypeid
LEFT JOIN names n ON e.namesid = n.namesid
WHERE (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 Recurring
FROM events e
LEFT 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.eventtypeid
LEFT JOIN names n ON e.namesid = n.namesid
WHERE (d.dt BETWEEN '2012/02/01' AND '2012/05/01')
ORDER BY d.dt;

猜你在找的MySQL相关文章