简体   繁体   English

如何在MySQL中特定日期范围内计数

[英]How to count at specific date range in mysql

I have created this mysql code to count at only specific date range, i dumped the output of my mysql code and here it is: 我创建了此mysql代码以仅在特定日期范围内进行计数,我转储了mysql代码的输出,此处为:

SELECT COUNT(
                IF(
                        DATE(q.date_emailed) BETWEEN '2014-02-01' AND '2014-02-28',
                        1,
                        0
                    )
        ) AS 'Feb', 
        COUNT(
                    IF(
                        DATE(q.date_emailed) BETWEEN '2014-03-01' AND '2014-03-31',
                        1,
                        0
                    )
         ) AS 'March', 
         COUNT(
                    IF(
                        DATE(q.date_emailed) BETWEEN '2014-01-01' AND '2014-01-31',
                        1,
                        0
                    )
         ) AS 'Jan' 
        FROM
            database.quotes q
        WHERE (DATE(q.date_emailed) BETWEEN '2014-01-01' AND '2014-03-31')

But this outputs same count for each month, in which i confirmed that february and march has zero counts. 但这每月输出相同的计数,其中我确认2月和3月计数为零。 What am I missing here? 我在这里想念什么?

COUNT() counts non-null values. COUNT()计算非空值。 You can fix your code by using sum() instead. 您可以改用sum()来修复代码。 You can also simplify it by removing the if statements: 您也可以通过删除if语句来简化它:

SELECT SUM(DATE(q.date_emailed) BETWEEN '2014-02-01' AND '2014-02-28') AS Feb, 
       SUM(DATE(q.date_emailed) BETWEEN '2014-03-01' AND '2014-03-31') AS March, 
       SUM(DATE(q.date_emailed) BETWEEN '2014-01-01' AND '2014-01-31') AS Jan
FROM database.quotes q
WHERE DATE(q.date_emailed) BETWEEN '2014-01-01' AND '2014-03-31';

In MySQL, a boolean result is treated as 0 for "false" and 1 for "true". 在MySQL中,布尔结果对于“ false”被视为0对于“ true”被视为1 This is a great convenience and allows you to use sum() to count the number of matches. 这是一个极大的方便,它允许您使用sum()来计算匹配数。

Note that I also removed the single quotes around the column names. 请注意,我还删除了列名周围的单引号。 Single quotes should be used for string and date constants, not for identifiers. 单引号应用于字符串和日期常量,而不用于标识符。

EDIT: 编辑:

You can have this query run faster by using an index on q.date_emailed . 通过使用q.date_emailed上的索引,可以使此查询运行更快。 However, I don't think the index will be used because of the date() function. 但是,我不认为由于date()函数会使用索引。 You can fix this by changing the logic: 您可以通过更改逻辑来解决此问题:

SELECT SUM(DATE(q.date_emailed) BETWEEN '2014-02-01' AND '2014-02-28') AS Feb, 
       SUM(DATE(q.date_emailed) BETWEEN '2014-03-01' AND '2014-03-31') AS March, 
       SUM(DATE(q.date_emailed) BETWEEN '2014-01-01' AND '2014-01-31') AS Jan
FROM database.quotes q
WHERE q.date_emailed >= '2014-01-01' AND
      q.date_emailed < '2014-04-01';

You should use SUM instead of COUNT if you want to SUM up the 0 and 1. 如果要累加0和1,则应使用SUM而不是COUNT。

The COUNT will COUNT the number of row, the SUM will sum up the values. COUNT将对行数进行计数,SUM将对这些值求和。

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

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