创建记录重复mysql

时间:2014-09-14 21:01:36

标签: mysql sql select

我正在创建一个机构的每周时间表,其中一个选项是将时间表复制到下周(如果,让我们说,只有1个课程更改)。

有一种方法可以通过一个查询执行此操作吗?类似的东西:

Duplicate * from school_entries Where `date` BETWEEN DATE('$date') AND DATE_ADD(DATE('$date'), INTERVAL 6 DAY) Set `date` = `date' + 7;

2 个答案:

答案 0 :(得分:3)

使用INSERT ... SELECT语句。 SELECT语句应该返回所有未更改的字段,并且您可以提供date_to_be_modified的新日期和其他值:

INSERT INTO school_entries
    date_to_be_modified AS date,
    (
        SELECT 
            other_fields,
            as_needed,
        FROM school_entries
        WHERE date BETWEEN start_date AND end_date
    )

答案 1 :(得分:1)

INSERT INTO school_entries (`date`,`secondfield`,...)
SELECT 
    `date` + 7 AS `date`,
    `secondfield`,
    ...
    /** 
     * YOU HAVE TO MANNUALLY SELECT ALL REQUIRED FIELDS
     * EXCEPT PRIMARY KEY, OR IF THERE IS ANY UNIQUE FIELDS
     * OTHERISE YOU'LL GET DUPLICATE ENTRY ERROR 
     */
FROM school_entries   
WHERE `date` BETWEEN DATE('$date') AND DATE_ADD(DATE('$date'), INTERVAL 6 DAY)