简体   繁体   English

sql查询和php脚本显示每日平均问题

[英]sql query and php script to show average questions per day

In my questions table, each post is timestamped (MM-DD-YYYY). 在我的问题表中,每个帖子都带有时间戳(MM-DD-YYYY)。 Each individual post has a QuestionID, and a response has both a QuestionID and ResponseID. 每个单独的帖子都有一个QuestionID,而回复则同时具有QuestionID和ResponseID。 I want to run a php script that will grab all questions (all posts where ResponseID = null) and display how many questions per day. 我想运行一个php脚本,它将捕获所有问题(所有帖子,其中ResponseID = null),并显示每天有多少个问题。

I'd like it to be avg questions per day since June 1, 2010. 自2010年6月1日起,我希望每天平均提出问题。

Your help is much appreciated. 非常感谢您的帮助。 Thank you! 谢谢!

This gets each day, and the number of questions: 这每天都在讨论,问题的数量:

select q.myTimestamp, count(*) 
from questionsTable q 
where q.myTimeStamp >= '6/1/2010'
and q.ResponseID IS NULL
group by myTimestamp

To get the average you need both the count of days, and sum of number of questions... I would probably do this with 2 queries to MySQL and calculate in PHP. 为了获得平均值,您既需要天数,又需要问题总数。...我可能会对MySQL进行2次查询并在PHP中进行计算。

$query1 = "select count(*) as totalCount from questionsTable where myTimestamp >= '6/1/2010' and ResponseID IS NULL";
$query2 = "select count(*) as totalDays from (select distinct myTimestamp from questionsTable where myTimestamp >= '6/1/2010' and ResponseID IS NULL) a";

$res1 = mysql_query($query1);
$res2 = mysql_query($query2);

$row1 = mysql_fetch_array($res1);
$row2 = mysql_fetch_array($res2);

$avgPostCount = ($row1['totalCount'] / $row2['totalDays']);

not optimal, but should work.. 不是最佳,但应该可以工作。

I made an assumption, that you wouldn't want to count days where there were no questions.. otherwise it could be simplified to one query using a mysql function to get the number of days since 6/1/2010. 我做出了一个假设,即您不想计算没有问题的天数。否则可以使用mysql函数简化为一个查询,以获取自2010年6月1日以来的天数。

SELECT
    AVG(NumQuestions)
FROM
    (
        SELECT
            COUNT(*) AS NumQuestions
        FROM
            Questions
        WHERE
            ResponseID IS NULL AND
            Question_Date >= '2010-06-01'
        GROUP BY
            Question_Date
    ) AS MySubquery

On second thought: This is silly. 再三考虑:这很愚蠢。 If you want to know the average number of questions per day since a given day, you may as well just count the number of questions (simple) and then divide by the number of days that have elapsed since then. 如果您想知道自给定日期以来每天的平均问题数,则不妨计算一下问题数(简单),然后除以此后经过的天数。 Also, the above query gives inaccurate results if there are days on which nobody asked a question. 此外,如果在几天中没有人问过问题,上述查询将给出错误的结果。

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

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