简体   繁体   English

如何使用php / MySQL做重复日期

[英]How to do recurring dates with php/MySQL

I have a table: 我有一张桌子:

table Event
- name
- description
- date (in format 2011-08-06 11:00:00)
- id

I need to create recurring events . 我需要创建重复事件

How can I best duplicate a row with mostly the same data (name, description) but change the date? 如何最好地复制具有几乎相同的数据(名称,描述)的行,但是更改日期?

Say, if I have an event that occurs August 6th, I would like to create three more events on - August 13, 20, 27 (every Saturday) 假设如果我有一个活动发生在8月6日,那么我想在8月13日,20日,27日(每个星期六)再创建三个活动

Also, is this something better accomplished with a mySQL query? 另外,使用mySQL查询可以更好地完成此操作吗? I am guessing so because of the format the date is in...but any advice would be appreciated. 我猜是因为日期格式所以...但是任何建议都将不胜感激。

MySQL lacks a generator functionality (which would allow to create arbitrary length recordsets from scratch), so if the number of records is fixed, you could do something like that: MySQL缺少生成器功能(这将允许从头开始创建任意长度的记录集),因此,如果记录数固定,则可以执行以下操作:

INSERT
INTO    event (name, description, date)
SELECT  name, description, date + INTERVAL n WEEK
FROM    (
        SELECT  1 AS n
        UNION ALL
        SELECT  2 AS n
        UNION ALL
        SELECT  3 AS n
        ) q
JOIN    event
ON      id = $myid

As you can see, the query layout depends on the number of events to create, with pure SQL there is no way to parameterize it. 如您所见,查询布局取决于要创建的事件数,而使用纯SQL则无法对其进行参数化。

You, however, can create a stored procedure and just call INSERT in a loop: 但是,您可以创建一个存储过程,并仅在循环中调用INSERT

CREATE PROCEDURE prc_fill_event (p_id INT, p_cnt INT)
BEGIN
        DECLARE _cnt INT;
        SET _cnt = 1;
        WHILE _cnt <= p_cnt DO
                INSERT
                INTO    event (name, description, date)
                SELECT  name, description, date + INTERVAL _cnt WEEK
                FROM    event
                WHERE   id = p_id;
                SET _cnt = _cnt + 1;
        END WHILE;
END

Depends. 要看。 If it's a recurring date that goes on into infinity, that's gonna be a lot of rows to create. 如果这是一个无穷无尽的重复日期,那么将会创建很多行。 Or you'd have to choose some limit on that, and that starts to complicate the problem. 否则,您必须为此选择一些限制,这会使问题复杂化。

Maybe it'd be better to have additional columns that describe recurring events, and then render them accordingly in your view layers. 也许最好有其他列描述重复发生的事件,然后在视图层中相应地呈现它们。

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

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