MySQL 查询返回来自相应列值的仅 NO 的数量
让我们首先创建一个表 -
mysql> create table DemoTable1829 ( Name varchar(20), isTopper ENUM('YES','NO') ); Query OK, 0 rows affected (0.00 sec)
使用 insert 命令向表中插入一些记录 -
mysql> insert into DemoTable1829 values('Chris','yes'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1829 values('David','yes'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1829 values('Mike','no'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1829 values('David','yes'); Query OK, 1 row affected (0.00 sec)
使用 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)
广告