MySQL query to get count of each fileid entry in a table with Id and FileIDs?


Let us first create a table −

mysql> create table DemoTable
(
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   FileID int
) AUTO_INCREMENT=100;
Query OK, 0 rows affected (1.36 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable(FileID) values(50);
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable(FileID) values(60);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable(FileID) values(50);
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable(FileID) values(70);
Query OK, 1 row affected (0.25 sec)
mysql> insert into DemoTable(FileID) values(60);
Query OK, 1 row affected (0.28 sec)
mysql> insert into DemoTable(FileID) values(50);
Query OK, 1 row affected (0.32 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+-----+---------+
| Id  | FileID  |
+-----+---------+
| 100 |      50 |
| 101 |      60 |
| 102 |      50 |
| 103 |      70 |
| 104 |      60 |
| 105 |      50 |
+-----+---------+
6 rows in set (0.00 sec)

Following is the query to get a count of each FileID entry in a table −

mysql> select FileID,COUNT(FieldId) AS EntryCount from DemoTable group by FileID;

This will produce the following output −

+---------+------------+
| FileID  | EntryCount |
+---------+------------+
|      50 |          3 |
|      60 |          2 |
|      70 |          1 |
+---------+------------+
3 rows in set (0.00 sec)

Updated on: 03-Oct-2019

47 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements