如何比较MySQL中的毫秒数与给定日期

时间:2012-02-28 13:56:12

标签: mysql timestamp unix-timestamp

使用

mysql  Ver 14.12 Distrib 5.0.45, for redhat-linux-gnu (i686) using readline 5.0

我有一个像这样定义的表:

+-----------------------+--------------+------+-----+---------+-------+
| Field                 | Type         | Null | Key | Default | Extra |
+-----------------------+--------------+------+-----+---------+-------+
| id                    | bigint(20)   | NO   | PRI |         |       | 
| user_id               | bigint(20)   | NO   | MUL |         |       | 
| directory_id          | bigint(20)   | NO   | MUL |         |       | 
| attribute_name        | varchar(255) | NO   |     |         |       | 
| attribute_value       | varchar(255) | YES  |     | NULL    |       | 
| attribute_lower_value | varchar(255) | YES  |     | NULL    |       | 
+-----------------------+--------------+------+-----+---------+-------+

选择我上次验证时间的attribute_value返回

+-------------------+-----------------+
| attribute_name    | attribute_value |
+-------------------+-----------------+
| lastAuthenticated | 1330380013284   | 
+-------------------+-----------------+

如果我使用http://www.epochconverter.com/验证该值,则说明

Assuming that this timestamp is in milliseconds:
GMT: Mon, 27 Feb 2012 22:00:13 GMT

但如果我尝试以下代码行

mysql> select from_unixtime('1330380013284');
+--------------------------------+
| from_unixtime('1330380013284') |
+--------------------------------+
| NULL                           | 
+--------------------------------+

任何人都知道我在这里缺少什么?

2 个答案:

答案 0 :(得分:6)

您需要除以1000以及删除引号,否则您的日期将被取消

SELECT FROM_UNIXTIME(theField/1000);

SELECT FROM_UNIXTIME(1330380013284/1000);

请参阅文档 - 注意MySQL方法返回SECONDS,而不是像你使用的那样毫秒:

MySQL docs for UNIX_TIMESTAMP

If called with no argument, returns a Unix timestamp 
(SECONDS since '1970-01-01 00:00:00' UTC) as an unsigned integer

答案 1 :(得分:0)

删除',您必须传递一个号码。