- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count number of occurrences of records in a MySQL table and display the result in a new column?
For this, use COUNT(*) along with GROUP BY clause. Let us first create a table −
mysql> create table DemoTable1942 ( Value int ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1942 values(1); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1942 values(2); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1942 values(3); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1942 values(2); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1942 values(3); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1942 values(3); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1942 values(1); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1942;
This will produce the following output −
+-------+ | Value | +-------+ | 1 | | 2 | | 3 | | 2 | | 3 | | 3 | | 1 | +-------+ 7 rows in set (0.00 sec)
Here is the query to count number of occurrences:
mysql> select Value, count(*) from DemoTable1942 group by Value;
This will produce the following output −
+-------+----------+ | Value | count(*) | +-------+----------+ | 1 | 2 | | 2 | 2 | | 3 | 3 | +-------+----------+ 3 rows in set (0.00 sec)
- Related Articles
- MySQL query to count occurrences of distinct values and display the result in a new column?
- Display the count of duplicate records from a column in MySQL and order the result
- Count duplicate ids and display the result in a separate column with MySQL
- How to round MySQL column with float values and display the result in a new column?
- How to implement Count (*) as variable from MySQL to display the number of records in a table?
- MySQL query to display the count of distinct records from a column with duplicate records
- MySQL query to get the length of all columns and display the result in a single new column?
- Divide numbers from two columns and display result in a new column with MySQL
- How do we count the total duplicate records in a column of MySQL table?
- Update a table in MySQL and display only the initials name in a new column
- MySQL query to get the count of values and display the count in a new column ordered in descending order
- MySQL query to group by names and display the count in a new column
- MySQL query to count the number of 0s and 1s from a table column and display them in two columns?
- Query to divide the values of two columns and display the result in a new column using MySQL wildcard?
- MySQL query to count the duplicate ID values and display the result in a separate column

Advertisements