Mysql触发器无法正常工作。将当前日期添加到where子句

时间:2013-07-12 03:47:37

标签: mysql triggers

我是使用MySQL的触发器的新手。

我通过考虑日期将副本行的触发器写入另一个表。

CREATE TRIGGER `new` AFTER INSERT ON `employee`
FOR EACH ROW
insert into employee_log (first_name, last_name)
select first_name, last_name from employee
where start_date = '2013-07-11'

我的员工表start_date格式为date。如何将当前日期设为start_date = '2013-07-12'

我在触发器中尝试start_date = now()但没有复制。

我的employee表格结构如下。

CREATE TABLE `employee` (
  `id` int(11) DEFAULT NULL,
  `first_name` varchar(30) DEFAULT NULL,
  `last_name` varchar(15) DEFAULT NULL,
  `start_date` date DEFAULT NULL,
  `end_date` varchar(10) DEFAULT NULL,
  `city` varchar(10) DEFAULT NULL,
  `description` varchar(15) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

在此表格中插入记录时触发检查start_date 今天/当前日期,然后将行复制到employee_log表格。

CREATE TABLE `employee_log` (
  `id` int(11) DEFAULT NULL,
  `first_name` varchar(50) DEFAULT NULL,
  `last_name` varchar(50) DEFAULT NULL,
  `start_date` date DEFAULT NULL,
  `end_date` date DEFAULT NULL,
  `city` varchar(50) DEFAULT NULL,
  `description` varchar(50) DEFAULT NULL,
  `Lasinserted` time DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

4 个答案:

答案 0 :(得分:0)

试试这个:

DELIMITER $$

CREATE TRIGGER `employee_new` AFTER INSERT 
ON `employee` FOR EACH ROW 
BEGIN
  IF (new.start_date >= currdate ()) 
  THEN 
  INSERT INTO employee_log (first_name, last_name) 
  VALUES
    (new.first_name, new.last_name) ;
  END IF ;
END $$

DELIMITER ;

答案 1 :(得分:0)

使用date_format函数将now()格式化为当前日期

CREATE TRIGGER `new` AFTER INSERT ON `employee`
FOR EACH ROW
insert into employee_log (first_name, last_name)
select first_name, last_name from employee
where start_date = DATE_FORMAT(now(),'%Y-%m-%d') /*DATE_FORMAT(now(),'%Y-%m-%d') represents current date as format %Y-%m-%d

答案 2 :(得分:0)

如果你想让你的触发器保持一个语句(意思是不改变分隔符并使用BEGIN...END阻止)并且仍然可以执行以下任务

CREATE TRIGGER `tg_employee_after_insert` 
AFTER INSERT ON `employee`
FOR EACH ROW
  INSERT INTO employee_log (id, first_name, last_name, end_date, city, description, Lasinserted)
  SELECT NEW.id, 
         NEW.first_name, 
         NEW.last_name, 
         NEW.end_date, 
         NEW.city, 
         NEW.description, 
         NOW()
    FROM dual
   WHERE NEW.start_date = CURDATE();

这是 SQLFiddle 演示

答案 3 :(得分:0)

您可以检查新添加的start_date是否为curdate()。如果'true'然后'insert'进入日志表。

示例

delimiter //

CREATE TRIGGER `new` AFTER INSERT ON `employee`
FOR EACH ROW BEGIN
  CASE WHEN NEW.start_date = CURRENT_DATE THEN
    INSERT
      INTO employee_log( first_name, last_name )
    VALUES ( NEW.first_name, NEW.last_name );
  END CASE;
END;
//

delimiter ;

但是从了解表结构及其名称,我假设您要记录employee表的最新添加记录。为此,您必须包含记录中的所有列。

示例

  CASE WHEN NEW.start_date = CURRENT_DATE THEN
    INSERT
      INTO employee_log( id, first_name, last_name, 
                         start_date, end_date,
                         city, description, Lasinserted )
    VALUES ( NEW.id, NEW.first_name, NEW.last_name, 
             NEW.start_date, NEW.end_date,
             NEW.city, NEW.description, NEW.lasinserted );
  END CASE;