通过“上周”、“上个月”搜索记录的选择查询可能是什么?和“去年”?

发布于 2024-12-15 12:24:28 字数 248 浏览 0 评论 0原文

我有一个系统,我需要在其中显示记录列表,这样我们就可以使用三个选项

  1. 上周
  2. 上个月
  3. 去年

我的表结构有一个字段调用createdate,它是BIGINT 输入并保存从 PHP 的 time() 函数获得的数据。

请指导我获取选择查询以按“上周”、“上个月”和“去年”搜索记录

谢谢

I have system in which I need to display the list of records such we are available with three options

  1. Last Week
  2. Last Month
  3. Last Year

My table structure has a field call createdate which is BIGINT type and saves the data obtained from PHP's time() function.

Please guide me to get the select query to search records By "Last Week", "Last Month" and "Last Year"

Thanks

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

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

发布评论

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

评论(4

夕色琉璃 2024-12-22 12:24:28

这应该可以解决问题:

SELECT * FROM my_table WHERE createdate BETWEEN DATE_SUB(NOW(), INTERVAL 1 week) AND NOW();    
SELECT * FROM my_table WHERE createdate BETWEEN DATE_SUB(NOW(), INTERVAL 1 month) AND NOW() ;   
SELECT * FROM my_table WHERE createdate BETWEEN DATE_SUB(NOW(), INTERVAL 1 year) AND NOW();

this should do the trick:

SELECT * FROM my_table WHERE createdate BETWEEN DATE_SUB(NOW(), INTERVAL 1 week) AND NOW();    
SELECT * FROM my_table WHERE createdate BETWEEN DATE_SUB(NOW(), INTERVAL 1 month) AND NOW() ;   
SELECT * FROM my_table WHERE createdate BETWEEN DATE_SUB(NOW(), INTERVAL 1 year) AND NOW();
请止步禁区 2024-12-22 12:24:28

许多给定的查询都受到索引可用性不佳的影响。如果您的 createdate 是在 unix 时间中,则比较也应该在 unix 时间中进行。

SELECT *
    FROM mytable
    WHERE createdate
        BETWEEN UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 7 DAY) AND UNIX_TIMESTAMP()

应该提供一种在 createdate 上使用索引范围的方法,并且不需要全表扫描。

Many of the given queries suffer from bad usability of indexes. If your createdate is in unix time, the comparison should happen as well in unix time.

SELECT *
    FROM mytable
    WHERE createdate
        BETWEEN UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 7 DAY) AND UNIX_TIMESTAMP()

should provide a way to use a range on the index over createdate and doesn't require a full table scan.

深居我梦 2024-12-22 12:24:28

我认为可能是这样的:

SELECT * FROM your_table
WHERE FROM_UNIXTIME(date_col) BETWEEN
    DATE_SUB(NOW(), INTERVAL 7 DAY) AND NOW()

SELECT * FROM your_table
WHERE FROM_UNIXTIME(date_col) BETWEEN
    DATE_SUB(NOW(), INTERVAL 1 MONTH) AND NOW()


SELECT * FROM your_table
WHERE FROM_UNIXTIME(date_col) BETWEEN
    DATE_SUB(NOW(), INTERVAL 1 YEAR) AND NOW()

I think it could be something like this:

SELECT * FROM your_table
WHERE FROM_UNIXTIME(date_col) BETWEEN
    DATE_SUB(NOW(), INTERVAL 7 DAY) AND NOW()

SELECT * FROM your_table
WHERE FROM_UNIXTIME(date_col) BETWEEN
    DATE_SUB(NOW(), INTERVAL 1 MONTH) AND NOW()


SELECT * FROM your_table
WHERE FROM_UNIXTIME(date_col) BETWEEN
    DATE_SUB(NOW(), INTERVAL 1 YEAR) AND NOW()
爱要勇敢去追 2024-12-22 12:24:28
SELECT * FROM mytable WHERE YEAR(FROM_UNIXTIME(createdate)) = YEAR(NOW())-1
SELECT * FROM mytable WHERE WEEK(FROM_UNIXTIME(createdate)) = WEEK(NOW())-1 AND YEAR(FROM_UNIXTIME(createdate)) = YEAR(NOW())
SELECT * FROM mytable WHERE MONTH(FROM_UNIXTIME(createdate)) = MONTH(NOW())-1 AND YEAR(FROM_UNIXTIME(createdate)) = YEAR(NOW())

请参阅周函数选项,例如一周的第一天。

SELECT * FROM mytable WHERE YEAR(FROM_UNIXTIME(createdate)) = YEAR(NOW())-1
SELECT * FROM mytable WHERE WEEK(FROM_UNIXTIME(createdate)) = WEEK(NOW())-1 AND YEAR(FROM_UNIXTIME(createdate)) = YEAR(NOW())
SELECT * FROM mytable WHERE MONTH(FROM_UNIXTIME(createdate)) = MONTH(NOW())-1 AND YEAR(FROM_UNIXTIME(createdate)) = YEAR(NOW())

See the week function for options like first day of week.

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