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

标签 mysql date phpmyadmin

我有表格时间表。

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. 计划|| 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)但不同日期的今日程序副本的正确查询。顺便说一句,我正在使用这样的 php MyAdmin 事件 enter image description here

最佳答案

考虑以下问题

架构

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

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

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

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

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

有关创建事件的详细示例,请参阅我写的 here .我已经做了一些,我相信其他人还有其他更好的。

关于mysql - 使用 phpMyAdmin 中的事件自动插入 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32578137/

相关文章:

mysql - 如何使用curdate实现查询?

php - 有没有更好的方法从 MySQL 数据库获取项目?

mysql - 通过 phpMyAdmin 将 23Mb .sql 文件导入 MySQL 时出现编码问题

apache - 本地主机错误代码 : 200 | Error in processing request (It seems like the connection to server has been lost.)

mysql - phpMyAdmin 无法连接到 MySQL 服务器

php - 将 ajax 值传递给 paypal

mysql - Amazon Web 服务远程 mysql 访问被拒绝 'user' @'localhost' ubuntu 16.04 服务器

jquery - 如何将 Q 语言 (KDB) 中的日期格式转换为 MM/DD

c# - 获取接下来的几天但不是周末,asp.net c#

javascript - 日期比较运算符 - 逻辑问题