简体   繁体   English

计算两个时间戳之间每个用户的记录数

[英]count number of records per user between two time stamps

I have two scenarios. 我有两种情况。

1: I want to count records from the table that have been entered today with particular userid. 1:我想统计今天从表中输入的具有特定用户ID的记录。 ie only the today entered stats shall be displayed. 即只显示今天输入的统计信息。

2: i want to display the records with one week time span with particular userid. 2:我想显示具有特定用户ID的一周时间跨度的记录。 ie every time it displays the records one week time interval should be taken. 即,每次它显示记录时,应采取一个星期的时间间隔。

Below is my query: 以下是我的查询:

$sql2=$Db1->query("SELECT COUNT(userid) AS total FROM tablename WHERE userid='$userid'");

to display the records 显示记录

<?php
while($row = mysql_fetch_array($sql2))
  {
  echo "<td>" . $row['total'] . "</td>";
 }
?>

I'm sure you have column with this date information. 我确定您有包含此日期信息的列。 You can then use: 然后,您可以使用:

SELECT
    DATE(your_date_column), 
    COUNT(userid) AS total 
FROM tablename 
WHERE userid='$userid'
AND your_date_column BETWEEN (NOW() - INTERVAL 1 WEEK) AND NOW()
GROUP BY DATE(your_date_column)

If you only want data of complete days then you've got to tweak the query a little bit, ie to exclude the first day of this interval. 如果您只需要整天的数据,则必须对查询进行一些调整,即排除此间隔的第一天。

If you want the totals of this week only without counts on daily basis you can use 如果您只希望本周的总数不包括每日计数,则可以使用

SELECT
    userid,
    COUNT(userid) AS total 
FROM tablename 
WHERE userid='$userid'
AND your_date_column BETWEEN (NOW() - INTERVAL 1 WEEK) AND NOW()
GROUP BY userid;

Note 注意

Please have a look the red box for the mysql_ extension and consider moving to mysqli or PDO. 请查看mysql_扩展名红色框,并考虑移至mysqli或PDO。 It's much better to use prepared statements with placeholders to bind your input values to than concatenation of sql strings. 使用带占位符的预处理语句将输入值绑定到SQL字符串串联要好得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM