MySQL 触发器:更新一条记录 table 其中同一行中的记录匹配 select 查询
MySQL Trigger: update record in one table where record in the same row match select query
MySQL 和数据库的新手,需要一些帮助。
我有 tables:
约会
- appointment_id 内部 PK , <-
- patient_id 整数,
- doctor_id 整数, <-
- appointment_time 日期时间。
队列
- appointment_id 内战,<-
- actual_time 日期时间。
queue_summary
- 日期日期时间,
- doctor_id 内部 PK , <-
- num_of_patients 整数。 <-
我需要编写一个触发器,通过向 queue_summary 中的 doctor_id 与新行重叠的行中的记录添加 +1 来更新 num_of_patients插入对应于约会的 queue
(以提取 doctor_id)。
例如。如果插入 queue
:
预约table状态:
需要更新:
我编写了以下 select 查询以将受 insertion/deletion 影响的 doctor_id 提取到队列中:
select appointment.doctor_id
from appointment as a
join queue as q
on a.appointment_id = q.appointment_id
where new.appointment_id = a.appointment_id;
并且我尝试如下编写触发器:
delimiter //
CREATE TRIGGER tr_insert
BEFORE INSERT ON queue
FOR EACH ROW
BEGIN
set queue_summary.num_of_patients = queue_summary.num_of_patients+1
where queue_summary.doctor_id = (
select appointment.doctor_id
from appointment as a
join queue as q
on a.appointment_id = q.appointment_id
where new.appointment_id = a.appointment_id
)
END;//
delimiter ;
但是没有运气......
我知道我在 where
部分有一些语法错误,但我无法让它工作......
请指教,
提前致谢!
触发器中的SET命令只能用于指向正在处理的行的NEW var。要更新另一个 table,您需要发出一个普通的 UPDATE 命令:
delimiter //
CREATE TRIGGER tr_insert
BEFORE INSERT ON queue
FOR EACH ROW
BEGIN
update queue_summary
set queue_summary.num_of_patients = queue_summary.num_of_patients+1
where queue_summary.doctor_id = (
select appointment.doctor_id
from appointment as a
join queue as q
on a.appointment_id = q.appointment_id
where new.appointment_id = a.appointment_id
)
END;//
delimiter ;
这就是为什么在 where 上出现语法错误的原因,这是因为 mysql set 命令不会在那里期望它,SET 只接受 var 名称和值。
MySQL 和数据库的新手,需要一些帮助。 我有 tables:
约会
- appointment_id 内部 PK , <-
- patient_id 整数,
- doctor_id 整数, <-
- appointment_time 日期时间。
队列
- appointment_id 内战,<-
- actual_time 日期时间。
queue_summary
- 日期日期时间,
- doctor_id 内部 PK , <-
- num_of_patients 整数。 <-
我需要编写一个触发器,通过向 queue_summary 中的 doctor_id 与新行重叠的行中的记录添加 +1 来更新 num_of_patients插入对应于约会的 queue
(以提取 doctor_id)。
例如。如果插入 queue
:
预约table状态:
需要更新:
我编写了以下 select 查询以将受 insertion/deletion 影响的 doctor_id 提取到队列中:
select appointment.doctor_id
from appointment as a
join queue as q
on a.appointment_id = q.appointment_id
where new.appointment_id = a.appointment_id;
并且我尝试如下编写触发器:
delimiter //
CREATE TRIGGER tr_insert
BEFORE INSERT ON queue
FOR EACH ROW
BEGIN
set queue_summary.num_of_patients = queue_summary.num_of_patients+1
where queue_summary.doctor_id = (
select appointment.doctor_id
from appointment as a
join queue as q
on a.appointment_id = q.appointment_id
where new.appointment_id = a.appointment_id
)
END;//
delimiter ;
但是没有运气......
我知道我在 where
部分有一些语法错误,但我无法让它工作......
请指教,
提前致谢!
触发器中的SET命令只能用于指向正在处理的行的NEW var。要更新另一个 table,您需要发出一个普通的 UPDATE 命令:
delimiter //
CREATE TRIGGER tr_insert
BEFORE INSERT ON queue
FOR EACH ROW
BEGIN
update queue_summary
set queue_summary.num_of_patients = queue_summary.num_of_patients+1
where queue_summary.doctor_id = (
select appointment.doctor_id
from appointment as a
join queue as q
on a.appointment_id = q.appointment_id
where new.appointment_id = a.appointment_id
)
END;//
delimiter ;
这就是为什么在 where 上出现语法错误的原因,这是因为 mysql set 命令不会在那里期望它,SET 只接受 var 名称和值。