简体   繁体   English

Mysql选择count distinct并选择distinct

[英]Mysql select count distinct and select distinct

So as example, in the picter bellow, I have simple table called students . 例如,在下面的照片中,我有一个叫做students简单表。 Some student has GPA = NULL. 有些学生的GPA = NULL。 I have a query to count DISTINCT GPA values : 我有一个查询来计算DISTINCT GPA值:

SELECT count( DISTINCT GPA ) FROM `students` 

在此输入图像描述

Result, as expected, 2. 结果,如预期,2。

But also, I thought, I can count using PHP, and just write: 但是,我想,我可以使用PHP,只写:

SELECT DISTINCT GPA FROM `students` 

But why the result is 3 lines(not 2 as expected)? 但为什么结果是3行(不是预期的2行)?

The Count function is defined to ignore NULL values. Count函数定义为忽略NULL值。 When you select DISTINCT rows, NULL is considered. 选择DISTINCT行时,会考虑NULL。 Thats why you get 3 rows. 这就是为什么你得到3行。 It is by design. 这是设计的。 See documentation for COUNT: http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_count 请参阅COUNT的文档: http//dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_count

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM