There is a question about MySQL\'s COUNT() aggregate function that keeps popping into my head time to time. I would like to get some explanation to why it is working the way
COUNT()
function accepts an argument, that is treated as NULL
or NOT NULL
. If it is NOT NULL
- then it increments the value, and doesn't do anything otherwise.
In your case expression value=4
is either TRUE
or FALSE
, obviously both true
and false
are not null, that is why you get 10.
but I am interested in a COUNT(condition) based solution.
The count
-based solution will be always slower (much slower), because it will cause table fullscan and iterative comparison of each value.