0

我有一个表,其中我将日期存储在 unix 时间戳中,例如 1286515961。

需要,选择当月的记录,例如'October 2010'。

我想直接在 mysql 查询中选择记录,以提高速度,但使用 unix 时间戳。

谁能帮我?已经谢谢了

4

2 回答 2

0

最简单的方法是使用FROM_UNIXTIME

SELECT *
FROM table
WHERE FROM_UNIXTIME(date) >= '2010-10-01'
  AND FROM_UNIXTIME(date) <  '2010-11-01'
于 2010-10-08T23:58:42.967 回答
0

Alec's answer is good

this would be:

SELECT * FROM table WHERE FROM_UNIXTIME(date) >= '2010-10-01' AND FROM_UNIXTIME(date) <  '2010-11-01'

but its actually faster to do it the other way round. convert your boundries to unix timestamp and then do the comparision. so the conversion needs not to be done for every row in the table but only for the boundries, then integers can be compared internally which is a lot faster than the translation to datetime objects... like so...

SELECT * FROM table 
WHERE date >= UNIX_TIMESTAMP('2010-10-01') 
AND date <  UNIX_TIMESTAMP('2010-11-01')

(untested)

于 2010-10-09T00:14:35.430 回答