繁体   English   中英

如何使用 if 语句 MySQL 触发删除和添加?

[英]How to make triggers for delete and adding with if statement MySQL?

有两个表的 Queue (appointment_id, actual_time) Queue_Summary (date, doctor_id, num_of_patients)

第一个是所有队列,第二个是每个医生在某个日期的队列数。 我需要构建一个更新 num_of_patients 的触发器,每次在队列中添加队列时,我都需要在该日期添加到医生 num_of_patients。 拆的时候也是。

我刚刚计算了给定医生 ID 和日期的队列数量,并将其分为两个触发器。 但我唯一的问题是我在哪里放置 if 语句来检查这个日期是否在 Queue_Summary 上,如果没有则添加它。

(PS - 我不是 100% 的,因为我的数据库有点关闭并且有很多问题,如果在 thoes 声明中有任何问题,我会更乐意知道)

delimiter //
 CREATE TRIGGER update_queue_summary 
    AFTER DELETE ON queue
    FOR EACH ROW
        BEGIN
            update queue_summary as qs set num_of_patient = ( 
                select count(appointment_id) 
                from queue as q join appointment as a on appointment_id
                where a.doctor_id=qs.doctor_id and date(qs.actual_time)=date(qs.date())
                group by appointment_id
                ) where doctor_id=qs.doctor_id and date(qs.actual_time)=date(qs.date());
       END;//
 delimiter ;
delimiter //
 CREATE TRIGGER update_queue_summary 
    AFTER insert ON queue
    FOR EACH ROW
        BEGIN
            update queue_summary as qs set num_of_patient = ( 
                select count(appointment_id) 
                from queue as q join appointment as a on appointment_id
                where a.doctor_id=qs.doctor_id and date(qs.actual_time)=date(qs.date())
                group by appointment_id
                ) where doctor_id=qs.doctor_id and date(qs.actual_time)=date(qs.date());
       END;//
 delimiter ;

您应该在触发器中执行存在测试。 例如

drop table if exists queue,queue_summary;
create table queue  (appointment_id int auto_increment primary key, doctor_id int,actual_time datetime);
create table Queue_Summary (date date, doctor_id int, num_of_patients int);

delimiter $$
create trigger ut after insert on queue
for each row 
begin
    if not exists (select 1 from queue_summary where date = date(new.actual_time) and doctor_id = new.doctor_id)  then
        insert into queue_summary values(date(new.actual_time),new.doctor_id,1);
    else
        update queue_summary
            set num_of_patients = num_of_patients + 1
            where date = date(new.actual_time) and doctor_id = new.doctor_id;
    end if;
end $$

delimiter ;

insert into queue (doctor_id,actual_time) values(1,'2020-05-03 09:00'),(1,'2020-05-03 09:30');

select * from queue;
select * from queue_summary;

MariaDB [sandbox]> select * from queue;
+----------------+-----------+---------------------+
| appointment_id | doctor_id | actual_time         |
+----------------+-----------+---------------------+
|              1 |         1 | 2020-05-03 09:00:00 |
|              2 |         1 | 2020-05-03 09:30:00 |
+----------------+-----------+---------------------+
2 rows in set (0.001 sec)

MariaDB [sandbox]> select * from queue_summary;
+------------+-----------+-----------------+
| date       | doctor_id | num_of_patients |
+------------+-----------+-----------------+
| 2020-05-03 |         1 |               2 |
+------------+-----------+-----------------+
1 row in set (0.001 sec)

删除触发器类似但更简单

delimiter $$
create trigger dt after delete on queue
for each row 
begin
    if exists (select 1 from queue_summary where date = date(OLD.actual_time) and doctor_id = old.doctor_id)  then
        update queue_summary
            set num_of_patients = num_of_patients - 1
            where date = date(old.actual_time) and doctor_id = old.doctor_id;
    end if;

end $$

delimiter ;

存在检查完全是装饰性的,因为如果没有要删除的内容,删除不会抱怨。

暂无
暂无

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

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