3

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:

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. What am I missing here?

2
  • What is the data type of date_emailed? Commented Mar 13, 2014 at 20:31
  • @Gordon Linoff: the datatype is datetime Commented Mar 13, 2014 at 20:34

2 Answers 2

6

COUNT() counts non-null values. You can fix your code by using sum() instead. You can also simplify it by removing the if statements:

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". This is a great convenience and allows you to use sum() to count the number of matches.

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. However, I don't think the index will be used because of the date() function. 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';
Sign up to request clarification or add additional context in comments.

2 Comments

I dont want to add any fields, i just want to count the number of records in the quotes table at that specific date ranges.
I didn't know what your SUM does but it worked. LOL Thanks a lot sir.
2

You should use SUM instead of COUNT if you want to SUM up the 0 and 1.

The COUNT will COUNT the number of row, the SUM will sum up the values.

3 Comments

They are mysql's if statements, I think 1 is for true and 0 is for false, am i right? why would i want to add them?
COUNT will count the number of rows, SUM will sum up the 1 and 0. Which is your required output?
I dont want to add any fields, i just want to count the number of records in the quotes table at that specific date ranges.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.