如何使用MySQL获取列中特定值的计数?
让我们首先创建一个表-
create table DemoTable ( Id int, Name varchar(100) );
使用插入命令在表中插入一些记录-
insert into DemoTable values(100,'John'); insert into DemoTable values(101,'Chris'); insert into DemoTable values(102,'Chris'); insert into DemoTable values(103,'Chris'); insert into DemoTable values(104,'David'); insert into DemoTable values(105,'Chris'); insert into DemoTable values(106,'Carol'); insert into DemoTable values(107,'Bob');
使用select语句显示表中的所有记录-
select *from DemoTable;
这将产生以下输出-
+------+-------+ | Id | Name | +------+-------+ | 100 | John | | 101 | Chris | | 102 | Chris | | 103 | Chris | | 104 | David | | 105 | Chris | | 106 | Carol | | 107 | Bob | +------+-------+ 8 rows in set (0.00 sec)
以下是查询以获取列中特定值的计数,即“Chris”在这里-
select count(*) from DemoTable where Name='Chris';
这将产生以下输出-
+----------+ | count(*) | +----------+ | 4 | +----------+ 1 row in set (0.00 sec)