gpt4 book ai didi

mysql - SQL - 日期字段限制(<=今天)

转载 作者:行者123 更新时间:2023-11-30 22:30:43 26 4
gpt4 key购买 nike

我有一个表格,其中包含成员(member)加入俱乐部时输入的日期。该列是沼泽标准,类型为 DATE。不需要时间。

基本上,我正在尝试找到一种 future 不可能的方式?

最佳答案

您可以使用触发器。下面是一个功能齐全的测试示例:

create table t(signup_date date not null);

delimiter //
create trigger ins_check_date before insert on t
for each row
begin
if (new.signup_date > current_date()) then
signal sqlstate '45000' set message_text = 'Date cannot be in the future';
end if;
end//

create trigger up_check_date before update on t
for each row
begin
if (new.signup_date > current_date()) then
signal sqlstate '45000' set message_text = 'Date cannot be in the future';
end if;
end//



delimiter ;

insert into t values (current_date());
insert into t values (current_date() - interval 1 day);
insert into t values (current_date() - interval 2 day);
insert into t values (current_date() + interval 1 day);

select * from t;
update t set signup_date = signup_date - interval 1 month
where signup_date = current_date() - interval 1 day;

update t set signup_date = signup_date + interval 1 month
where signup_date = current_date();
select * from t;

关于mysql - SQL - 日期字段限制(<=今天),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33959636/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com