mysql count vs sum

Solutions on MaxInterview for mysql count vs sum by the best coders in the world

showing results for - "mysql count vs sum"
Roberto
06 Oct 2020
1+----+------+
2| id | vote |
3+----+------+
4|  1 |    1 |       COUNT(vote) = 5 
5|  2 |   -1 |		SUM(vote) = 1  = (-2 + 3 = 1)
6|  3 |    1 |			  
7|  4 |   -1 |	#Sum is doing the mathematical sum, whereas count simply 
8|  5 |    1 |	#counts any value as 1 regardless of what data type.
9+----+------+
10
11/**
12The first query returns the number of times the condition is true, because true is 1 and false is 0.
13The 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.
14To 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:
15*/
16SELECT COUNT(case when USER_NAME = 'JoeBlow' then 'no matter what' else NULL end) 
17from your_table