In MySQL - What is the difference between using SUM or COUNT?
SELECT SUM(USER_NAME = 'JoeBlow')
SELECT COUNT(USER_NAME = 'JoeBlow')
The first query returns the number of times the condition is true, because true
is 1
and false
is 0
.
The second query returns the complete record count because count()
does not care about the content inside it, as long as the content is NOT NULL. Because count(1)
and count(0)
are still values and both get counted.
To get the correct return value for the second query you would have to make the result of the condition be null
(instead of 0
) to not being counted. Like this:
SELECT COUNT(case when USER_NAME = 'JoeBlow' then 'no matter what' else NULL end)
from your_table
Or simply remove the else
part from the case
statement which automatically makes the else
part null
.