mysql count distinct null使用注意事項 1 用一個例子來講解一個問題,現在又一個庫表hello,表內容如下: www.2cto.com id name 1 Null 2 Null 3 Null 4 Null 5 Null hello表一共兩個字段:id和name,name is null。 www.2cto.com 第一條SQL:SELECT COUNT(id) FROM hello; 查詢結果:5,正確。 第二條SQL:SELECT COUNT(*) FROM hello; 查詢結果:5,正確。 第三條SQL:SELECT COUNT(name) FROM hello; 查詢結果:0,錯誤。 第四條SQL:SELECT COUNT(DISTINCT id,name) FROM hello;查詢結果:0,錯誤。 2 第二條SQL和第三條SQL查詢錯誤的原因: 2.1 COUNT(), MIN(), and SUM() ignore NULL values. 2.2 The exception to this is COUNT(*), which counts rows and not individual column values. 2.3 For example, the following statement produces two counts. The first is a count of the number of rows in the table, and the second is a count of the number of non-NULL values in the age column: mysql> SELECT COUNT(*), COUNT(age) FROM person;