使用 phpMyAdmin 中的事件自动插入 MySQL

Auto insert MySQL using Events in phpMyAdmin

我有 table 时间表。

CREATE TABLE Schedule (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
Program VARCHAR(30) NOT NULL,
Time DATETIME NOT NULL)

一天有很多节目,例如:

星期一

  1. 程序 x || 17:00
  2. 程序y || 18:00

.....

星期二

  1. 编程一个|| 10:00
  2. 程序b || 12:00

.....

直到星期天。

我想每天触发,下周查看当天是否有数据。所以在星期一 14/09/2015 将检查下一个星期一 21/09/2015。如果当天没有数据,则插入上周一的数据。

也许逻辑或者伪代码会是这样

IF (Current date + 7 days IS NULL)
Then
INSERT INTO SCHEDULE (program, time)
VALUES ( (Select Program FROM SCHEDULE Where Day(Sysdate()) = Day(Time)),
         (select Time FROM SCHEDULE Where Day(sysdate()=Day(Time))+7 Day) )

我的问题是我不知道插入具有相同时间 (HH:MM) 但不同的今日程序副本的正确查询 Date.BTW 我正在使用 php 像这样的 MyAdmin 事件

考虑以下因素

架构

-- 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);

-- 为下周所有基于日期的节目插入一行,大概是本周

查询

-- 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;

结果

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 |
+----+---------------------+----------+-------------+

运行 再说一遍:

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;

结果

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 |
+----+---------------------+----------+-------------+

这利用了 insert on duplicate key update 功能的 mysql 功能。请参阅手册页 here。如果要插入的行已经存在,则更新发生。这就是我展示 counterDemo 专栏的原因。这样,就没有重复的数据。 counterDemo 只是一个视觉效果。

create table 底部的 unique key(theDate,Program) 是它起作用的原因。当 mysql 看到基于此的重复项时,它会强制更新(而不是插入)。再次注意上面段落中手册页的 link。

date_add 的另一个手册页也请查看。

一次吞下很多东西,但它可以使您的数据保持干净。您需要将此应用到您使用 phpmyadmin 创建的活动中。

有关创建事件的详细示例,请参阅我写的 。我做了几个,我相信还有其他更好的。