MySQL创建具有查询和聚合功能的触发器

标签 mysql

我是 mySQL 的新手,正在努力创建触发器。

我想要的是一个触发器,它在向表 A 插入新行之前进行检查。如果表 A 在过去 1 小时内有超过 50 条数据,则删除新行。

以下代码是我想出的,但这是不正确的。请帮我修复它。

create trigger before_A_insert
before insert on A
for each row
begin
    declare temp int default 0
    set temp = (
        select  count(*)
        from    A
        where   id = new.id and timestampdiff(minute, new.starttime, starttime) < 60
    )
    if temp = 50 then
        set new.id = null
    end if
end;

最佳答案

这样使用:

DELIMITER $$
CREATE TRIGGER before_A_insert
BEFORE INSERT ON A FOR EACH ROW
begin

declare temp int default 0

     select  count(*) into @temp from A where id = new.id and timestampdiff(minute, new.starttime, starttime) < 60);
    if @temp = 50 then
        set new.id = null;
    end if;

END;
$$
DELIMITER ;

关于MySQL创建具有查询和聚合功能的触发器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42547458/

相关文章:

MySQL 查询运行很长时间将日期时间转换为日期

php - 从 Db 生成 JSON bool 值

mysql - 如果一行的总和等于 X,则选择行

mysql - 从 sql 查询到等效的 mysql

MySQL 优化一个请求

php mysql在数据库的文本区域中存储换行符

php - 在 Android 中对 Json 输出进行排序

java - 如何在SQL语句中使用Arraylist的每个元素作为列值(迭代Arraylist的每个元素)?

数据库还原时不存在 mysql.proc

php - 查询以获取相关产品按匹配标签排序