MySQL清单中特定项目的不同数量

要查找不同数量的特定项目,请COUNT()与GROUP BY子句一起使用。让我们首先创建一个表-

mysql> create table DemoTable1854
     (
     Name varchar(20)
     );

使用插入命令在表中插入一些记录-

mysql> insert into DemoTable1854 values('John-Smith');
mysql> insert into DemoTable1854 values('Chris-Brown');
mysql> insert into DemoTable1854 values('Adam-Smith');
mysql> insert into DemoTable1854 values('John-Doe');
mysql> insert into DemoTable1854 values('John-Smith');

使用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)