Mysql sql触发器确保约会间隔30天不工作

Mysql sql触发器确保约会间隔30天不工作,mysql,sql,triggers,Mysql,Sql,Triggers,尝试创建一个触发器,确保约会间隔至少30天,但这似乎不起作用: delimiter // create trigger time_between_doses before insert on appointment for each row begin if datediff((select appointment_time from appointment where new.patientSSN = patientSSN), new.appointment_time) < 30

尝试创建一个触发器,确保约会间隔至少30天,但这似乎不起作用:

delimiter //
create trigger time_between_doses
before insert on appointment
for each row
begin 
if datediff((select appointment_time from appointment where new.patientSSN = patientSSN), 
new.appointment_time) < 30
    THEN SIGNAL SQLSTATE '45000'
    SET MYSQL_ERRNO = 9996,
    MESSAGE_TEXT = 'Not 30 days between appointments for this patient';
end if;
end //
delimiter ;

要检查的查询将使用的查询存在。这30天意味着什么还不是100%清楚。您可能想知道这是否返回任何行:

select a.*
from appointment a
where new.patientSSN = a.patientSSN and
      new.appointment_time < a.appointment_time + interval 30 day;
有一件事尚不清楚,那就是30天的限制是否会成为过去,也会成为未来。以及它是否只适用于过去的任命,还是也适用于未来的任命

您的版本是一个等待发生的错误,因为您使用的子查询预期只有一个值。如果子查询返回多行,那么代码将生成一个错误。上述逻辑应与exists一起使用