MySQL查询返回对应列值中仅NO值的计数
让我们首先创建一个表-
mysql> create table DemoTable1829 ( Name varchar(20), isTopper ENUM('YES','NO') );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1829 values('Chris','yes'); mysql> insert into DemoTable1829 values('David','yes'); mysql> insert into DemoTable1829 values('Mike','no'); mysql> insert into DemoTable1829 values('David','yes');
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1829;
这将产生以下输出-
+-------+----------+ | Name | isTopper | +-------+----------+ | Chris | YES | | David | YES | | Mike | NO | | David | YES | +-------+----------+ 4 rows in set (0.00 sec)
这是返回仅NO值计数的查询-
mysql> select Name,sum(isTopper='no') from DemoTable1829 group by Name;
这将产生以下输出-
+-------+--------------------+ | Name | sum(isTopper='no') | +-------+--------------------+ | Chris | 0 | | David | 0 | | Mike | 1 | +-------+--------------------+ 3 rows in set (0.00 sec)