简体   繁体   中英

Mysql select count distinct and select 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` 

在此输入图像描述

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)?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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