0

我是使用 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

4 回答 4

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 ;

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

示例

  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;
于 2014-06-11T06:58:15.613 回答
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
于 2013-07-12T05:45:26.677 回答
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 ;
于 2013-07-12T05:37:47.673 回答
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演示

于 2013-07-12T16:29:27.540 回答