如何选择 UNIX 日期=“2010”在mysql查询中?

发布于 2024-09-29 03:51:49 字数 328 浏览 2 评论 0原文

到目前为止,这是我的代码:

SELECT `date`, title, category, url
FROM cute_news
WHERE category = '4'
ORDER BY `date` DESC

我想根据年份制作页面,例如 2010 年、2009 年、2008 年等。 数据库将日期保存为 UNIX_Timestamp。不确定如何使用 Year 参数查询记录集?

WHERE unix_timestamp(YEAR) = '2010' or something???

提前致谢。我很困惑。

Here is my code so far:

SELECT `date`, title, category, url
FROM cute_news
WHERE category = '4'
ORDER BY `date` DESC

I want make pages based on the year, like, 2010 , 2009, 2008 and so on.
The database saves the date as UNIX_Timestamp. Not sure how to query a recordset with a Year parameter?

WHERE unix_timestamp(YEAR) = '2010' or something???

Thanks in advance. I'm baffled.

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

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

发布评论

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

评论(3

清欢 2024-10-06 03:51:49

您需要类似 WHERE YEAR(FROM_UNIXTIME(date_field)) = 2010 的内容。

You'll want something like WHERE YEAR(FROM_UNIXTIME(date_field)) = 2010.

煞人兵器 2024-10-06 03:51:49

您可以使用 FROM_UNIXTIME() 函数,但请注意,这不会使用 date 列上的索引(如果存在):

... WHERE YEAR(FROM_UNIXTIME(`date`)) = 2010

您的查询为 sargable,您可以使用 UNIX_TIMESTAMP() 函数改为:

... WHERE `date` >= UNIX_TIMESTAMP('2010-01-01 00:00:00') AND 
          `date` < UNIX_TIMESTAMP('2011-01-01 00:00:00')

You can use the FROM_UNIXTIME() function, but be aware that this will not use an index on the date column, if one exists:

... WHERE YEAR(FROM_UNIXTIME(`date`)) = 2010

For your query to be sargable, you could use the the UNIX_TIMESTAMP() function instead:

... WHERE `date` >= UNIX_TIMESTAMP('2010-01-01 00:00:00') AND 
          `date` < UNIX_TIMESTAMP('2011-01-01 00:00:00')
带上头具痛哭 2024-10-06 03:51:49

尝试:

WHERE YEAR(FROM_UNIXTIME(`date`)) = 2010;

Try:

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