在 MySQL 中获取不同的值并对其进行计数

mysqlmysqli database

要获取不同的值并对其进行计数,您可以使用 GROUP BY 子句。

语法如下

select yourColumnName,count(*) as anyAliasName from yourTableName group by yourColumnName;

为了理解上述语法,让我们创建一个表。创建表的查询如下

mysql> create table GroupByAndCountDemo
   -> (
   -> ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> ClientName varchar(100)
   -> );
Query OK, 0 rows affected (0.64 sec)

使用 insert 命令在表中插入一些记录。 查询语句如下 −

mysql> insert into GroupByAndCountDemo(ClientName) values('John');
Query OK, 1 row affected (0.18 sec)
mysql> insert into GroupByAndCountDemo(ClientName) values('Carol');
Query OK, 1 row affected (0.14 sec)
mysql> insert into GroupByAndCountDemo(ClientName) values('Sam');
Query OK, 1 row affected (0.15 sec)
mysql> insert into GroupByAndCountDemo(ClientName) values('Sam');
Query OK, 1 row affected (0.16 sec)
mysql> insert into GroupByAndCountDemo(ClientName) values('John');
Query OK, 1 row affected (0.51 sec)
mysql> insert into GroupByAndCountDemo(ClientName) values('John');
Query OK, 1 row affected (0.15 sec)
mysql> insert into GroupByAndCountDemo(ClientName) values('John');
Query OK, 1 row affected (0.13 sec)
mysql> insert into GroupByAndCountDemo(ClientName) values('Sam');
Query OK, 1 row affected (0.08 sec)
mysql> insert into GroupByAndCountDemo(ClientName) values('Sam');
Query OK, 1 row affected (0.47 sec)
mysql> insert into GroupByAndCountDemo(ClientName) values('Sam');
Query OK, 1 row affected (0.09 sec)
mysql> insert into GroupByAndCountDemo(ClientName) values('John');
Query OK, 1 row affected (0.13 sec)
mysql> insert into GroupByAndCountDemo(ClientName) values('John');
Query OK, 1 row affected (0.18 sec)
mysql> insert into GroupByAndCountDemo(ClientName) values('John');
Query OK, 1 row affected (0.10 sec)
mysql> insert into GroupByAndCountDemo(ClientName) values('David');
Query OK, 1 row affected (0.12 sec)
mysql> insert into GroupByAndCountDemo(ClientName) values('Maxwell');
Query OK, 1 row affected (0.17 sec)
mysql> insert into GroupByAndCountDemo(ClientName) values('Maxwell');
Query OK, 1 row affected (0.15 sec)

使用 select 语句显示表中的所有记录。查询如下 −

mysql> select *from GroupByAndCountDemo;

输出如下

+----------+------------+
| ClientId | ClientName |
+----------+------------+
|        1 | John       |
|        2 | Carol      |
|        3 | Sam        |
|        4 | Sam        |
|        5 | John       |
|        6 | John       |
|        7 | John       | 
|        8 | Sam        |
|        9 | Sam        |
|       10 | Sam        |
|       11 | John       |
|       12 | John       |
|       13 | John       |
|       14 | David      |
|       15 | Maxwell    |
|       16 | Maxwell    |
+----------+------------+
16 rows in set (0.00 sec)

现在让我们获取不同的值并使用以下查询对其进行计数

mysql> select ClientName,count(*) as TotalCount from GroupByAndCountDemo group by ClientName;

以下是输出 −

+------------+------------+
| ClientName | TotalCount |
+------------+------------+
| John       |          7 |
| Carol      |          1 |
| Sam        |          5 |
| David      |          1 |
| Maxwell    |          2 |
+------------+------------+
5 rows in set (0.00 sec)

相关文章