如何使用if语句MySQL生成delete和adding的触发器?
有两个表的队列(预约id、实际时间)队列摘要(日期、医生id、患者数量) 第一个是所有的队列,第二个是某个日期每个医生的队列数量。我需要构建一个触发器来更新患者的数量,每次在队列中添加队列时,我都需要在该日期将患者的数量添加到医生。拆卸时也是如此 我刚刚计算了给定医生id和日期的队列数量,将其分为两个触发器。 但我唯一的问题是,我应该将if语句放在何处,该语句检查此日期是否在Queue_Summary上,如果不在,则添加它 (另一方面,我不是100%支持thoes,因为我的数据库有点混乱,存在很多问题,如果thoes有任何问题,我会很高兴让他们知道)如何使用if语句MySQL生成delete和adding的触发器?,mysql,sql,Mysql,Sql,有两个表的队列(预约id、实际时间)队列摘要(日期、医生id、患者数量) 第一个是所有的队列,第二个是某个日期每个医生的队列数量。我需要构建一个触发器来更新患者的数量,每次在队列中添加队列时,我都需要在该日期将患者的数量添加到医生。拆卸时也是如此 我刚刚计算了给定医生id和日期的队列数量,将其分为两个触发器。 但我唯一的问题是,我应该将if语句放在何处,该语句检查此日期是否在Queue_Summary上,如果不在,则添加它 (另一方面,我不是100%支持thoes,因为我的数据库有点混乱,存在很
您应该在触发器中执行生存测试。比如说
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 ;
存在性检查完全是装饰性的,因为如果没有什么可删除的,删除不会抱怨。你没有接受@gordon linhoff昨天在回答中提供的任何内容-为什么?当我问他如何工作时,他没有重播,我只看到两个没有if的更新。。。无论需要做什么,这都是双重更新。我如何使用它?答案是否提供了令人满意的更新方式?你为什么不使用新的。而且很老。价值观请在问题中添加表定义和示例数据。不,因为这不是我的问题。我不明白你不能同时删除和插入。。。不管更新是否是我所需要的,它都是两个更新,一个是加1,另一个是减。意味着它什么也没做。如果我弄错了,请在我询问有关代码的更多信息时告诉我一些应该是新的。某些内容可能来自插入或更新行。假设select为您提供了所需的内容,那么应该更新哪一行?
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 ;