Consider the following
Schema
-- drop table Schedule;
create table Schedule
( id int auto_increment primary key,
theDate datetime not null, -- sorry, stay away from KEYWORDS and RESERVED WORD
Program VARCHAR(30) NOT NULL,
counterDemo int not null,
unique key(theDate,Program) -- prevents duplicates at the combo-level
);
-- truncate table Schedule;
-- note I am skipping the time part of the date below
insert Schedule(theDate,Program,counterDemo) values
('2015-09-15','ProgramA',1),
('2015-09-15','ProgramB',1),
('2015-09-16','ProgramA',1),
('2015-09-16','ProgramB',1);
-- insert a row for next week all programs based on a date, presumably this week
The query
-- without aliases, we seem to get the 1052 error: Ambiguous error
insert into Schedule(theDate,Program,counterDemo)
select date_add(t2.theDate,interval 1 week),t2.Program,1 from Schedule t2 where t2.theDate='2015-09-15'
on duplicate key update Schedule.counterDemo=Schedule.counterDemo+1;
The results
select * from schedule;
+----+---------------------+----------+-------------+
| id | theDate | Program | counterDemo |
+----+---------------------+----------+-------------+
| 1 | 2015-09-15 00:00:00 | ProgramA | 1 |
| 2 | 2015-09-15 00:00:00 | ProgramB | 1 |
| 3 | 2015-09-16 00:00:00 | ProgramA | 1 |
| 4 | 2015-09-16 00:00:00 | ProgramB | 1 |
| 5 | 2015-09-22 00:00:00 | ProgramA | 1 |
| 6 | 2015-09-22 00:00:00 | ProgramB | 1 |
+----+---------------------+----------+-------------+
run it again:
insert into Schedule(theDate,Program,counterDemo)
select date_add(t2.theDate,interval 1 week),t2.Program,1 from Schedule t2 where t2.theDate='2015-09-15'
on duplicate key update Schedule.counterDemo=Schedule.counterDemo+1;
The results
select * from schedule;
+----+---------------------+----------+-------------+
| id | theDate | Program | counterDemo |
+----+---------------------+----------+-------------+
| 1 | 2015-09-15 00:00:00 | ProgramA | 1 |
| 2 | 2015-09-15 00:00:00 | ProgramB | 1 |
| 3 | 2015-09-16 00:00:00 | ProgramA | 1 |
| 4 | 2015-09-16 00:00:00 | ProgramB | 1 |
| 5 | 2015-09-22 00:00:00 | ProgramA | 2 |
| 6 | 2015-09-22 00:00:00 | ProgramB | 2 |
+----+---------------------+----------+-------------+
This utilizes the mysql feature of insert on duplicate key update
feature. See Manual page here. If the row to be inserted already exists, the update occurs. That is why I showed the counterDemo column. That way, no duplicate data. The counterDemo was just a visual that it works.
The unique key(theDate,Program)
at the bottom of the create table
is what makes this work. When mysql sees a duplicate based on that, it forces the update (as opposed to an insert). Note again the link to the manual page in the above paragraph.
Another manual page for date_add too look at.
It's a lot to swallow at once, but it keeps your data clean. You need to work this into your event you created with phpmyadmin.
For a somewhat detailed example of Create Event, see this I wrote up here. I have done a few, there are other better ones I am sure by others.