创建带有默认时间的字段 [英] Create field with default as epoch time

查看:80
本文介绍了创建带有默认时间的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为一个表创建一个timestamp字段,该表的行需要在一定时间后过期.如果我使用以下内容:

I need to create a timestamp field for a table who's rows need to expire after a certain amount of time. If I use the following:

`timestamp` TIMESTAMP DEFAULT NOW(),

它以一种人类可读的格式显示时间,如果我可以将时间设置为纪元时间,那么将容易得多,这样我就可以用几秒钟来计算.有没有一种方法可以创建一个字段,当默认情况下以纪元时间创建行时,该字段将显示当前时间?谢谢!

It shows the time in a human readable format, it would be a lot easier if I could have it in epoch time so I can calculate with seconds. Is there a way I can create a field that will display the current time when a row is created in epoch time by default? Thanks!

推荐答案

您可能希望使用

You may want to use the UNIX_TIMESTAMP() function in your SELECT statements, as in the following example:

CREATE TABLE test_tb (`timestamp` TIMESTAMP DEFAULT NOW());
INSERT INTO test_tb VALUES (DEFAULT);

SELECT UNIX_TIMESTAMP(`timestamp`) epoch, `timestamp` FROM test_tb;
+------------+---------------------+
| epoch      | timestamp           |
+------------+---------------------+
| 1281834891 | 2010-08-15 03:14:51 |
+------------+---------------------+
1 row in set (0.00 sec)

这篇关于创建带有默认时间的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆