Php-MySql查询,根据日期时间选择数据
我想获取具有相同日期的行数。但一秒钟肯定是可以忽略不计的。
例如 ; 2012-01-03 13:12:28 和 2012-01-03 13:12:27 应该被视为相同。
(我的表名称是 myTable
,日期时间列名称是 date
)
我希望你能帮助我..
I would like to get number of rows that have same date. But one second must be negligible.
For example ; 2012-01-03 13:12:28 and 2012-01-03 13:12:27 should be perceived as the same.
( I have a table name is myTable
and the datetime column name is date
)
I hope you will help me..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
它很简单:
如果您想从变量构建它,您可以这样做:
编辑:根据下面的评论,以下 SQL 应该返回您想要的内容。请注意,如果日期包含“2012-01-03 13:12:28”,则 :27 和 :28 都会被计入。
It's as simple as:
If you want to build it up from a variable, you could do this:
EDIT: As per your comment below, the following SQL should return what you're after. Note that if a date contains '2012-01-03 13:12:28' it will be counted for both :27 and :28.
对于这种情况,我们使用带有别名的表的自连接...并且由于您的条件是日期之间的差异不应超过 1,所以...
我假设您已将日期存储为 mysql datetime...执行“-”操作..
For such cases we use self join of a table with aliases...and as your condition is the difference between the dates should not be more than 1 here it goes...
I presume that you have stored date as mysql datetime...to perform "-" operation..
尝试一次:
或者对于与组相关的 COUNT:
Try this once:
Or for a group-related COUNT:
如果您只需要计算每个日期的行数,那么这很简单 GROUP BY:
但是根据您的问题,您不仅需要计算日期内的行数,而且还不需要计算单个事件,而是计算一系列事件。在这种情况下,您需要定义系列到底是什么。假设一秒内发生的两个事件应算作一个。如果有四个彼此距离为 0.7 的事件怎么办?在这种情况下,我建议分两步计算系列。
首先 - 按时间排序事件:
然后在应用程序端,您可以计算两个记录之间的间隔,如果两个关闭事件之间的距离超过一秒,则增加计数器。
不要忘记在
date
字段上添加索引以加快此查询速度。If you only need to count number of rows per date it would be simple GROUP BY:
But by your question you are not only need to count rows within a date, but also count not a single events but it's series. In this case you will need to define what exactly series are. Say two events within one second should be counted as one. What if there are four events with 0.7 distance between each other? In this case I would suggest to count series in two steps.
First - order events by time:
Then on the application side you can count the gap between two records and increase counter if distance between two close events is more than one second.
Don't forget to add index on
date
field to speed up this query.