如何使用日期字段作为unix时间戳在mysql中选择一个时期?

发布于 2024-09-26 17:41:21 字数 160 浏览 2 评论 0原文

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

需要选择月份中的记录,例如“2010 年 10 月”。

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

谁能帮助我吗?已经谢谢了

I have a table with a field where I store the date in unix timestamp, eg 1286515961.

Need, select the records that are in the month, eg 'October 2010'.

I would like to select records directly in mysql query, for speed, but using unix timestamp.

Can anyone help me? Thanks already

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

牵强ㄟ 2024-10-03 17:41:21

亚历克的答案很好,

这将是:

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

但实际上反过来会更快。将您的边界转换为 unix 时间戳,然后进行比较。因此不需要对表中的每一行进行转换,而只需对边界进行转换,然后可以在内部比较整数,这比转换为日期时间对象要快得多...就像这样...

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

(未经测试)

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)

稀香 2024-10-03 17:41:21

最简单的方法是使用 FROM_UNIXTIME

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

The easiest way is to use FROM_UNIXTIME:

SELECT *
FROM table
WHERE FROM_UNIXTIME(date) >= '2010-10-01'
  AND FROM_UNIXTIME(date) <  '2010-11-01'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文