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


要获得不同值并对它们进行计数,可以使用 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)

更新于:2019 年 7 月 30 日

440 次浏览

开始您的 职业生涯

完成课程后获得认证

开始
广告