Reputation: 15
I have a table of music tracks with a length field that stores information on how long each individual track is. I'm trying to query to database to find out the maximum track length, minimum track length and I want the return the number of tracks which have a length listed (IS NOT NULL).
Here is the statement:
SELECT MAX(length), MIN(length), COUNT(length)
FROM tracks WHERE length IS NOT NULL;
The problem is the value COUNT() returns is the exact same whether I include the IS NOT NULL operator at the end or not. The table has around 25 entries that are NULL so surely I should be getting a lower result for COUNT() when I add the NOT NULL operator to the end of the statement?
Upvotes: 0
Views: 43
Reputation: 15057
BACKTICKS are your friend: length is a function name in MySQL and if you want o use it as field name you must put it in backticks like this:
SELECT MAX(`length`), MIN(`length`), COUNT(`length`)
FROM tracks WHERE `length` IS NOT NULL;
Upvotes: 0
Reputation: 38
The documentation says that count will ignore null columns http://dev.mysql.com/doc/refman/5.7/en/counting-rows.html
Including the where clause will not make a difference.
Upvotes: 1