开发者

SQL: Updating using timestamp

I have a table with a column called 'updated'. When a new row is CREATED, the column automatically inserts the current time. However, I'd like to UPDATE the column as well..any input on how to do this? Here's my update statement (doesn't have the 'updated' column yet):

$update = mysql_query("UPDATE documents SET company = '$company',claimnumber = '$claimnumber',fullna开发者_JS百科me = '$fullname',dateofloss = '$dateofloss',foruser = '$foruser' "."WHERE doc_id =".$doc_id);


Use a trigger, something like this:

create trigger updated_is_now before update on documents
for each row set NEW.updated = now();

Then you can send in your usual SQL UPDATE statements and the trigger will, effectively, add updated = now() to the SET clause.


If you want the updated column to be updated on update, assign NOW() to it in your query.


ALTER TABLE `documents` 
ADD COLUMN `UpdatedDate` TIMESTAMP DEFAULT CURRENT_TIMESTAMP 
ON UPDATE CURRENT_TIMESTAMP NOT NULL;

Edit: To change...

ALTER TABLE `documents` 
MODIFY `updated` TIMESTAMP DEFAULT CURRENT_TIMESTAMP 
ON UPDATE CURRENT_TIMESTAMP NOT NULL;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜