简体   繁体   English

MySQL触发器大于条件

[英]mysql trigger for greater than condition

Is it possible to create trigger for greater than condition. 是否可以创建大于条件的触发器。 Suppose I have a column temperature and new values are stored in that column after every 15 seconds. 假设我有一列温度,并且每隔15秒将新值存储在该列中。 So if the value becomes greater than predefined value(for example 100 degrees) a trigger is generated. 因此,如果该值变得大于预定义的值(例如100度),则会生成触发器。 Is it possible to do it in mysql? 有可能在mysql中做到吗?

MySql doesn't implement conditional triggers like Oracle does, for example in Oracle: MySql不会像Oracle那样实现条件触发器,例如在Oracle中:

CREATE TRIGGER triggername AFTER INSERT ON tablename FOR EACH ROW
WHEN ( condition )
BEGIN 
   ........
END;

the trigger is fired only when condition is meet. 仅在满足condition时才触发触发器。

In MySql the trigger is fired every time. 在MySql中,每次都会触发触发器。
However, it is still possible to use an conditional IF statement inside the trigger and perform some actions only when some condition is meet, for example: 但是,仍然可以在触发器内使用条件IF语句,并且仅在满足某些条件时才执行某些操作,例如:

Create table temperature(
  id int primary key auto_increment,
  temperature decimal( 5, 1 )
  );

Create table temp_above_100(
  id int primary key auto_increment,
  temperature decimal( 5, 1 )
  );

CREATE TRIGGER triggername 
AFTER INSERT ON temperature FOR EACH ROW
BEGIN
  IF new.temperature >= 100 THEN
     INSERT INTO temp_above_100( temperature )
     VALUES ( new.temperature );
  END IF;
END;/

INSERT INTO temperature( temperature ) 
VALUES (10), (20), (100), (120 );

click this link: http://www.sqlfiddle.com/#!2/6a930/1 to see results of this example. 单击此链接: http : //www.sqlfiddle.com/#!2/6a930/1以查看此示例的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM