douguo7431
2014-02-02 09:07
浏览 45
已采纳

Mysql选择count distinct并选择distinct

So as example, in the picter bellow, I have simple table called students. Some student has GPA = NULL. I have a query to count DISTINCT GPA values :

SELECT count( DISTINCT GPA ) FROM `students` 

enter image description here

Result, as expected, 2.

But also, I thought, I can count using PHP, and just write:

SELECT DISTINCT GPA FROM `students` 

But why the result is 3 lines(not 2 as expected)?

图片转代码服务由CSDN问答提供 功能建议

例如,在下面的picter中,我有一个名为 students 的简单表。 有些学生的GPA = NULL。 我有一个查询来计算DISTINCT GPA值:

  SELECT count(DISTINCT GPA)FROM`schools` 
   
 
 <  p>   
 
 

结果,如预期,2。

但是,我想,我可以使用PHP,只需写:

  SELECT DISTINCT GPA FROM`schools` 
    
 
 

但为什么结果是3行(不是预期的2)?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doufei1852 2014-02-02 09:11
    已采纳

    The Count function is defined to ignore NULL values. When you select DISTINCT rows, NULL is considered. Thats why you get 3 rows. It is by design. See documentation for COUNT: http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_count

    点赞 打赏 评论

相关推荐 更多相似问题