2012-02-28 38 views
2

使用比較毫秒如何在MySQL帶有給定日期

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       | 
+--------------------------------+ 

任何人誰立即知道我在這裏失蹤以下行?

+0

+1是很清楚。但aswer很容易:) – vulkanino 2012-02-28 14:00:20

回答

6

您需要通過1000分以及刪除引號,否則你的日期是遙遠

SELECT FROM_UNIXTIME(theField/1000); 

SELECT FROM_UNIXTIME(1330380013284/1000); 

參見文檔 - 注意MySQL的方法返回秒,不毫秒就像你正在使用:

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 
+0

啊,tnx!還有一種情況是我沒有RTFM :-) – rhellem 2012-03-02 13:24:42

0

刪除',你必須通過一個數字。

+0

select from_unixtime('133​​0380013284'/'1000'); – gaRex 2012-02-28 14:30:08

相關問題