MySQL查询以获取具有ID和FileID的表中每个FileID条目的计数?
让我们首先创建一个表-
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, FileID int ) AUTO_INCREMENT=100;
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable(FileID) values(50); mysql> insert into DemoTable(FileID) values(60); mysql> insert into DemoTable(FileID) values(50); mysql> insert into DemoTable(FileID) values(70); mysql> insert into DemoTable(FileID) values(60); mysql> insert into DemoTable(FileID) values(50);
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+-----+---------+ | Id | FileID | +-----+---------+ | 100 | 50 | | 101 | 60 | | 102 | 50 | | 103 | 70 | | 104 | 60 | | 105 | 50 | +-----+---------+ 6 rows in set (0.00 sec)
以下是获取表中每个FileID条目计数的查询-
mysql> select FileID,COUNT(FieldId) AS EntryCount from DemoTable group by FileID;
这将产生以下输出-
+---------+------------+ | FileID | EntryCount | +---------+------------+ | 50 | 3 | | 60 | 2 | | 70 | 1 | +---------+------------+ 3 rows in set (0.00 sec)