The question is about the MySQL aggregation function COUNT (), which constantly appears in me. I would like some explanation why it works as it is.
When I started working with MySQL, I quickly found out that its COUNT (condition) only works correctly if the condition also contains OR NULL at the end. In the case of more complex COUNT conditions, it was an empirical process to figure out where to specify it exactly. In MSSQL, you don’t need this OR NULL to get the correct results, so I would like to know an explanation. So here is an example.
It allows you to have a very basic table with the following structure and data:
CREATE TABLE test ( `value` int(11) NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1; INSERT INTO test (value) VALUES(1); INSERT INTO test (value) VALUES(4); INSERT INTO test (value) VALUES(5); INSERT INTO test (value) VALUES(6); INSERT INTO test (value) VALUES(4); INSERT INTO test (value) VALUES(4); INSERT INTO test (value) VALUES(5); INSERT INTO test (value) VALUES(2); INSERT INTO test (value) VALUES(8); INSERT INTO test (value) VALUES(1);
Scenario: I would like to calculate how many lines I have, where value = 4. The obvious solution would be to filter it with WHERE and do COUNT (*), but I'm interested in the solution-based COUNT condition (condition).
So the solution that comes to my mind:
SELECT COUNT(value=4) FROM test
The result is 10. This is obviously not true.
Second attempt with OR NULL:
SELECT COUNT(value=4 OR NULL) FROM test
The result is 3. This is correct.
Can someone explain the logic of this? Is this some kind of bug in MySQL or is there a logical explanation why I need to add this weird looking OR NULL to the end of the COUNT clause to get the correct result?
CodeTwice
source share