Home >Database >Mysql Tutorial >MySQL中在含有GROUP BY的SELECT语句中显示COUNT()为0的结果_MySQL

MySQL中在含有GROUP BY的SELECT语句中显示COUNT()为0的结果_MySQL

WBOY
WBOYOriginal
2016-06-01 13:30:271670browse

bitsCN.com

MySQL中在含有GROUP BY的SELECT语句中显示COUNT()为0的结果

 

mysql> select * from t1;+-------+------+| level | name |+-------+------+|     1 | a    ||     1 | b    ||     1 | c    ||     2 | a    ||     2 | b    ||     4 | a    ||     5 | b    |+-------+------+7 rows in set (0.00 sec)mysql> select * from t2;+------+| id   |+------+|    1 ||    2 ||    3 ||    4 ||    5 |+------+5 rows in set (0.02 sec)mysql> select t2.id as level,count(case when name is not null then 1 end) as count    ->   from t2 left join t1 on t2.id = t1.level    -> group by t2.id;+-------+-------+| level | count |+-------+-------+|     1 |     3 ||     2 |     2 ||     3 |     0 ||     4 |     1 ||     5 |     1 |+-------+-------+5 rows in set (0.00 sec)

 

 

bitsCN.com
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn