简体   繁体   English

使用count()输出每月以及每月的总条目

[英]Using count() to output total entries per month as well as the month

I have tried a bit of searching, and as a result found the count() function to get me closer to my goal, but have stopped a bit. 我尝试了一些搜索,结果找到了count()函数使我更接近目标,但又停了一下。 Here's what my code looks like now: 这是我的代码现在的样子:

$sql = "SELECT COUNT( * ) as Count
FROM AttendanceDB
WHERE YEAR( class_time ) =  '2015'
AND studentid = '$studentid'
AND furikae = 0
GROUP BY MONTH( class_time )";

Then I use echo $row['Count']; 然后我用echo $row['Count']; to output the data. 输出数据。

This allows me to total the number of rows per month for the year of 2015 and then output the answer. 这使我可以总计2015年每月的行数,然后输出答案。 Now what I want to do is output the month in each row along with the total. 现在,我想做的是输出每一行中的月份以及总数。 I could just preformat everything, but there are some studentids which have entries beginning partway through the year, which means that the first months don't output any results. 我可以对所有内容进行预格式化,但是有些学生ID会在一年中开始输入,这意味着前几个月不会输出任何结果。

How can I SELECT both the month from the DB as well as use the count function at the same time? 如何同时从数据库中选择月份以及同时使用计数功能? Thanks 谢谢

You could use an agregator function 您可以使用聚合函数

 $sql = "SELECT COUNT( * ),max(YOUR MONTH COLUMN) as MONTH as Count FROM AttendanceDB WHERE YEAR( class_time ) = '2015' AND studentid = '$studentid' AND furikae = 0 GROUP BY MONTH( class_time )"; 

Here is what I think will help you: 我认为这对您有帮助:

$sql = "SELECT MONTH( class_time ) AS month, COUNT( * ) as Count
FROM AttendanceDB
WHERE YEAR( class_time ) =  '2015'
AND studentid = '$studentid'
AND furikae = 0
GROUP BY MONTH( class_time )";

How about: 怎么样:

$sql = "SELECT MONTH(class_time) AS m, COUNT(*) AS c
    FROM AttendanceDB WHERE YEAR(class_time) = '2015'
    AND studentid = '$studentid'
    AND furikae = 0
    GROUP BY m";

This way you'll have the month as a separate column, which will also show in the result as $row['m'] and the count per month as $row['c'] . 这样,您将把月份作为单独的列,该列还将在结果中显示为$row['m'] ,而每月计数为$row['c'] (I avoid the same names as the MySQL function names, hence the m and c .) (我避免使用与MySQL函数名称相同的名称,因此避免使用mc 。)

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

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