MySQL触发器有多个if子句

时间:2018-08-27 17:59:05

标签: mysql sql triggers phpmyadmin

我想达成这样的目标:

插入数据时,它将检查日期列,将其与当前日期进行比较,这将在另一列中写入内容。示例:

Today 27/08/2018
Date 27/08/2018
Result: Same Day

Today 27/08/2018
Date 26/08/2018
Result: Before Today

Today 27/08/2018
Date 28/08/2018
Result: After Today

我已经尝试过了,但是不起作用:

//CREATE TRIGGER trigger_update_status_before_insert 
BEFORE INSERT 
ON tablex
FOR EACH ROW BEGIN
  IF (NEW.date < CURDATE()) THEN
        SET NEW.status = 'Before Today'
  END IF

  IF (NEW.date = CURDATE()) THEN
        SET NEW.status = 'Same Day'
  END IF

  IF (NEW.date > CURDATE()) THEN
        SET NEW.status = 'After Today'
  END IF
END;//

phpmyadmin分隔符://

1 个答案:

答案 0 :(得分:0)

error in your SQL syntax creating MySQL trigger @ p.campbell的答案解决了这个问题。我刚刚删除了半列,它起作用了。