从SQL中的时间戳,从今天,昨天,本周,本月以及两个日期php mysql之间选择记录 [英] From the timestamp in SQL, selecting records from today, yesterday, this week, this month and between two dates php mysql

查看:227
本文介绍了从SQL中的时间戳,从今天,昨天,本周,本月以及两个日期php mysql之间选择记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望这是一个简单的解决方案,但我试图过滤以下数据: -

Hopefully, it's an easy solution, but I am trying to filter the data for the following:-


  • 今天

  • 昨天

  • 本周

  • 本月

  • 两个日期之间。

  • Today
  • Yesterday
  • This Week
  • This Month
  • In between two dates.

从数据库获取的日期基本上是一个时间戳。

The date I get from the database is basically a timestamp.

这是我试过的: -

This is what I tried:-


  • 今天

  • Today

SELECT n.title, COUNT(*) AS times FROM node_view_count WHERE timestamp > DATE_SUB(NOW(), INTERVAL 1 DAY)


  • 昨天

  • Yesterday

    SELECT n.title, COUNT(*) AS times FROM node_view_count WHERE timestamp > DATE_SUB(NOW(), INTERVAL 7 DAYS)
    


  • ...

    没有真正的工作。

    任何想法?

    推荐答案

    如果您仅按日期选择,请根据 CURDATE (仅返回日期)而不是 NOW (返回日期和时间)。这些例子将在日间范围内捕获所有时间:

    If you're selecting by date only, base your calculations on CURDATE (which returns date only) rather than NOW (which returns date and time). These examples will catch all times within the day ranges:


    • 今天: WHERE timestamp> = CURDATE() / code>

    • 昨天: WHERE timestamp> = DATE_SUB(CURDATE(),INTERVAL 1 DAY)AND timestamp< CURDATE()

    • 本月: WHERE timestamp> = DATE_SUB(CURDATE(),INTERVAL DAYOFMONTH(CURDATE()) - 1 DAY)

    • 在2013年6月3日和2013年6月7日的两个日期之间(注意结束日期如何指定为6月8日,而不是6月7日): WHERE timestamp> ='2013-06-03'AND timestamp< '2013-06-08'

    • Today: WHERE timestamp >= CURDATE()
    • Yesterday: WHERE timestamp >= DATE_SUB(CURDATE(), INTERVAL 1 DAY) AND timestamp < CURDATE()
    • This month: WHERE timestamp >= DATE_SUB(CURDATE(), INTERVAL DAYOFMONTH(CURDATE())-1 DAY)
    • Between the two dates 3 June 2013 and 7 June 2013 (note how the end date is specified as 8 June, not 7 June): WHERE timestamp >= '2013-06-03' AND timestamp < '2013-06-08'

    本周取决于你开始一周的那一天;我会把它留给你您可以使用 DAYOFWEEK 函数调整 CURDATE()到适当的范围。

    The "this week" depends on which day you start your week; I'll leave that to you. You can use the DAYOFWEEK function to tweak CURDATE() to the proper ranges.

    附录:OP的列类型为 INTEGER ,存储UNIX时间戳,我的回答假定列类型为 TIMESTAMP 。以下是使用UNIX时间戳记值执行所有相同操作的方法,并且如果列被编入索引仍然保持优化(因为如果 TIMESTAMP 列被索引,上面的答案将会执行)。 ..

    Addendum: OP's column type was INTEGER, storing a UNIX timestamp, and my answer assumed the column type was TIMESTAMP. Here's how to do all the same things with a UNIX timestamp value and still maintain optimization if the column is indexed (as the answers above will do if the TIMESTAMP column is indexed)...

    基本上,解决方案是将开头和/或结束日期打包在 UNIX_TIMESTAMP 功能:

    Basically, the solution is to just wrap the beginning and/or ending dates in the UNIX_TIMESTAMP function:


    • 今天: WHERE timestamp> = UNIX_TIMESTAMP(CURDATE())

    • 昨天: WHERE timestamp> = UNIX_TIMESTAMP(DATE_SUB(CURDATE(),INTERVAL 1 DAY))AND AND UNIX_TIMESTAMP(CURDATE())

    • 本月: WHERE timestamp> = UNIX_TIMESTAMP(DATE_SUB(CURDATE(),INTERVAL DAYOFMONTH ()) - 1天))

    • 在2013年6月3日和2013年6月7日的两个日期之间(注意结束日期如何指定为6月8日, 6月7日): WHERE timestamp> = UNIX_TIMESTAMP('2013-06-03')AND timestamp< UNIX_TIMESTAMP('2013-06-08')

    • Today: WHERE timestamp >= UNIX_TIMESTAMP(CURDATE())
    • Yesterday: WHERE timestamp >= UNIX_TIMESTAMP(DATE_SUB(CURDATE(), INTERVAL 1 DAY)) AND timestamp < UNIX_TIMESTAMP(CURDATE())
    • This month: WHERE timestamp >= UNIX_TIMESTAMP(DATE_SUB(CURDATE(), INTERVAL DAYOFMONTH(CURDATE())-1 DAY))
    • Between the two dates 3 June 2013 and 7 June 2013 (note how the end date is specified as 8 June, not 7 June): WHERE timestamp >= UNIX_TIMESTAMP('2013-06-03') AND timestamp < UNIX_TIMESTAMP('2013-06-08')

    这篇关于从SQL中的时间戳,从今天,昨天,本周,本月以及两个日期php mysql之间选择记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆