Click here to Skip to main content
16,004,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have a table called work_events ( id, date_time, title). It has 1 row: (1,2024-09-07 11:00, GroupMeeting)

No, i want to copy that row like 51 times. the only thing is that the date on each row moves on 7 days....so basiclly I want the same row recurring every week...as this is actually shown in a calendar. However, it has to be a copy of rows...so each event has its own row...the id needs to increase IS there an a way to do this using sql?


What I have tried:

I have no idea where to start but I want to do this using an sql query...
Posted

1 solution

I haven't tried this but, looking at your table, something like this might work:
SQL
INSERT INTO work_events (date_time, title)
WITH RECURSIVE sequence(n) AS (
    SELECT 1
    UNION ALL
    SELECT n + 1 FROM sequence WHERE n < 51
)
SELECT DATE_ADD(we.date_time, INTERVAL (7 * s.n) DAY) as date_time, we.title
FROM work_events we
CROSS JOIN sequence s
WHERE we.id = 1;
Note that this is making the assumption that your id field is auto-incrementing.

We are using a Common Table Expression (CTE)[^] here to do the heavy lifting of the processing, making use of the fact that we have a predictable week numbering to apply here.
 
Share this answer
 
Comments
Mark Donners 2022 6-Sep-24 10:28am    
Thank you for your suggestion..however, when i insert this as sql script in my phpmyadmin.....i get an error around line 2: the name of the CTE was expected ( near sequence)
Pete O'Hanlon 6-Sep-24 16:17pm    
What version of MySQL are you using? I just tried a variant in MySQL 8 and it works fine.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900