MYSQL:
DELIMITER $$
CREATE TRIGGER `Activation_code` BEFORE UPDATE ON `user_users` FOR EACH ROW BEGIN
(CASE
WHEN (OLD.activation_code_time < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 15 MINUTE) AND OLD.activation_code <> NEW.activation_code)
THEN SET NEW.activation_code = (SELECT(ROUND((RAND() * (999999-100000))+100000))), NEW.activation_code_time = (SELECT(UNIX_TIMESTAMP()))
ELSE SET NEW.activation_code = OLD.activation_code, NEW.activation_code_time = OLD.activation_code_time
END);
(CASE
WHEN (OLD.forgotten_password_time < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 15 MINUTE) AND OLD.txt_key <> NEW.txt_key)
THEN SET NEW.forgotten_password_time = (SELECT(UNIX_TIMESTAMP()))
ELSE SET NEW.forgotten_password_time = OLD.forgotten_password_time
END);
END
$$
DELIMITER ;
错误:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'CASE
WHEN (OLD.activation_code_time < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTE' at line 2
任何人都可以告诉我如何解决此错误?为什么这个功能不起作用。我可以通过使用一个case和多个来解决这个问题,当一个语句带有else语句吗?
答案 0 :(得分:0)
使用if
代替case
。
DELIMITER $$
CREATE TRIGGER `ACTIVATION_CODE` BEFORE UPDATE ON `user_users`
FOR EACH ROW
BEGIN
IF OLD.ACTIVATION_CODE_TIME < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 15 MINUTE)) AND OLD.ACTIVATION_CODE != NEW.ACTIVATION_CODE THEN
SET NEW.ACTIVATION_CODE = (ROUND((RAND() * (999999-100000))+100000));
SET NEW.ACTIVATION_CODE_TIME = UNIX_TIMESTAMP();
ELSE
SET NEW.ACTIVATION_CODE = OLD.ACTIVATION_CODE;
SET NEW.ACTIVATION_CODE_TIME = OLD.ACTIVATION_CODE_TIME;
END IF;
IF OLD.forgotten_password_time < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 15 MINUTE)) AND OLD.txt_key != NEW.txt_key THEN
SET NEW.forgotten_password_time = UNIX_TIMESTAMP();
ELSE
SET NEW.forgotten_password_time = OLD.forgotten_password_time;
END IF;
END$$
DELIMITER ;
答案 1 :(得分:0)
您无法为SET
块下的字段应用多个CASE
值
而是首先在字段上调用SET
以根据CASE
设置值。
以下更改可能会对您有所帮助。
DELIMITER $$
CREATE TRIGGER `Activation_code` BEFORE UPDATE ON `user_users`
FOR EACH ROW
BEGIN
set NEW.activation_code = (
CASE WHEN
OLD.activation_code_time < UNIX_TIMESTAMP( DATE_SUB(NOW(), INTERVAL 15 MINUTE) )
AND OLD.activation_code <> NEW.activation_code
THEN ROUND( ( RAND() * (999999-100000) ) + 100000 )
ELSE OLD.activation_code
END
);
set NEW.activation_code_time = (
CASE WHEN
OLD.activation_code_time < UNIX_TIMESTAMP( DATE_SUB(NOW(), INTERVAL 15 MINUTE) )
AND OLD.activation_code <> NEW.activation_code
THEN UNIX_TIMESTAMP()
ELSE OLD.activation_code_time
END
);
set NEW.forgotten_password_time = (
CASE WHEN
OLD.forgotten_password_time < UNIX_TIMESTAMP( DATE_SUB(NOW(), INTERVAL 15 MINUTE) )
AND OLD.txt_key <> NEW.txt_key
THEN UNIX_TIMESTAMP()
ELSE OLD.forgotten_password_time
END
);
END;
$$
DELIMITER ;