0
我試圖創建一個表,該表設置了創建後2周內的到期日期。接下來的SELECT語句給了我這個紀錄:如何將計算的時間戳設置爲默認值MySQL
(SELECT TIMESTAMPADD(WEEK,2,(CURRENT_TIMESTAMP)));
| 2014-04-21 18:08:52 |
但在創建表不允許我把任何表達式默認值:
create table test (somename varchar(32), expiry TIMESTAMP DEFAULT (SELECT TIMESTAMPADD(WEEK,2,(CURRENT_TIMESTAMP))));
如何才能做到這一點? --update - 我試試這個觸發,但我的語法是錯誤的:
CREATE TRIGGER expiryset AFTER INSERT ON test BEGIN DECLARE exp TIMESTAMP; SET @exp := (SELECT TIMESTAMPADD(WEEK,2,(CURRENT_TIMESTAMP))); UPDATE test SET expiry = @exp WHERE somename = ; END;
這個工作,但我需要得到觸發前插入的somename值/列....我怎麼能得到這個?
[ON INSERT trigger](https://dev.mysql.com/doc/refman/5.0/en/create-trigger.html)? – bishop
我想我可以用觸發器做到這一點,但即使在這裏我也沒有太多經驗。這看起來如何?爲什麼我得到語法錯誤:CREATE TRIGGER expiryset after INSERT ON test FOR EACH ROW BEGIN UPDATE test SET expiry =(SELECT TIMESTAMPADD(WEEK,2,(CURRENT_TIMESTAMP))); END; – user1000622
請在您的OP中更新,並附上確切的錯誤。 :) – bishop