使用 MySQL 统计列表中特定项目的不同数量
要查找特定项目的不同数量,请使用 COUNT() 以及 GROUP BY 子句。让我们首先创建一个表 −
mysql> create table DemoTable1854 ( Name varchar(20) ); Query OK, 0 rows affected (0.00 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable1854 values('John-Smith');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1854 values('Chris-Brown');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1854 values('Adam-Smith');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1854 values('John-Doe');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1854 values('John-Smith');
Query OK, 1 row affected (0.00 sec)使用 select 语句显示表中的所有记录 −
mysql> select * from DemoTable1854;
这将产生以下输出 −
+-------------+ | Name | +-------------+ | John-Smith | | Chris-Brown | | Adam-Smith | | John-Doe | | John-Smith | +-------------+ 5 rows in set (0.00 sec)
以下是获取列表中特定项目不同数量的查询 −
mysql> select Name,count(Name) from DemoTable1854 where Name like 'John-%' group by Name;
这将产生以下输出 −
+------------+-------------+ | Name | count(Name) | +------------+-------------+ | John-Smith | 2 | | John-Doe | 1 | +------------+-------------+ 2 rows in set (0.00 sec)
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP