2014-02-05 41 views
0

我對MySQL事件驗證碼:MYSQL - 創建事件刪除過期的記錄

CREATE EVENT DeleteToken 
ON SCHEDULE EVERY 1 MINUTE 
DO 
BEGIN 
DELETE FROM 
`password_recovery` 
WHERE `date_expires` < 
CURRENT_TIMESTAMP(); 
END 

當我運行它,我得到以下錯誤:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 8 

date_exiresdatetime與格式YYYY-MM-DD HH:MM:SS CURRENT_TIMESTAMP()有什麼問題。

我也試過CURRENT_TIMESTAMP但它給了我同樣的錯誤。

回答

3

我建議你使用這個查詢 -

CREATE EVENT DeleteToken 
ON SCHEDULE EVERY 1 MINUTE 
DO 
DELETE FROM `password_recovery` WHERE `date_expires` < CURRENT_TIMESTAMP(); 

否則,當你使用BEGIN..END條款,你應該使用DELIMITER命令 -

DELIMITER $$ 
CREATE EVENT DeleteToken 
ON SCHEDULE EVERY 1 MINUTE 
DO 
BEGIN 
    DELETE 
    FROM `password_recovery` 
    WHERE `date_expires` < CURRENT_TIMESTAMP(); 
END$$ 
DELIMITER ; 
+0

它正在工作:)。 (不知道關於DELIMITER $$)謝謝你的幫助:) – Sasha

0

只需使用Now()代替CURRENT_TIMESTAMP()

+0

還是什麼都沒有嘗試 - 報同樣的錯誤。 – Sasha

0

代替

ON SCHEDULE EVERY 1 MINUTE 

嘗試

ON SCHEDULE 1 MINUTE 

完全schecule西港島線是這樣的:

CREATE EVENT DeleteToken 
ON SCHEDULE 1 MINUTE 
DO 
    BEGIN 
    DELETE FROM `password_recovery` WHERE `date_expires` < CURRENT_TIMESTAMP(); 
    END 
+0

當我試圖改變時,我得到了新的錯誤。似乎每個1分鐘都需要這樣寫。 – Sasha

1

你需要設置分隔符在查詢:

DELIMITER | 

CREATE EVENT 
... 
CURRENT_TIMESTAMP(); 
END| 

DELIMITER ; 

如果使用phpMyAdmin,請設置分隔符字段而不是手動設置分隔符。

0

使用DELIMITER

DELIMITER $$ 
CREATE EVENT DeleteToken 
ON SCHEDULE EVERY 1 MINUTE 
DO 
BEGIN 
DELETE FROM 
`password_recovery` 
WHERE `date_expires` < 
Now(); 
END$$ 
DELIMITER ;