我要求用户的当前密码与最后五个使用的密码不匹配。过期的密码将存储在与此类似的表中。
################################################################
# table_A #
################################################################
# id # label # attr_string # attr_datetime # u_id #
################################################################
# 1 # expired_pass # some_hash # YYYY-MM-DD HH:MM:SS # 1 #
# 2 # expired_pass # some_hash # YYYY-MM-DD HH:MM:SS # 1 #
# 3 # expired_pass # some_hash # YYYY-MM-DD HH:MM:SS # 1 #
# 4 # expired_pass # some_hash # YYYY-MM-DD HH:MM:SS # 1 #
# 5 # expired_pass # some_hash # YYYY-MM-DD HH:MM:SS # 1 #
################################################################
现在我想要一个触发器,在输入检查时是否已存储> = 5个过期密码,如果是,则删除最旧的密码。这就是我到目前为止所提出的。
DELIMITER //
CREATE TRIGGER removePass AFTER INSERT ON `user_attributes`(
SET @user_id := SELECT u_id FROM `user_attributes` WHERE id = LAST_INSERT_ID()//
SET @num := SELECT COUNT(id) FROM `user_attributes` WHERE u_id = @user_id AND label = 'expired_password'//
IF @num >= 5 THEN
DELETE FROM `user_attributes`
WHERE id IN(
SELECT id
FROM `user_attributes`
WHERE u_id = @user_id
AND label = 'expired_pass'
ORDER BY attr_datetime DESC
LIMIT 1
)
END IF
)//
DELIMITER ;