- 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
MySQL query to select the values having multiple occurrence and display their count
For this, use GROUP BY HAVING clause. Let us first create a table −
mysql> create table DemoTable673( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Value int ); Query OK, 0 rows affected (0.59 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable673(Value) values(10); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable673(Value) values(20); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable673(Value) values(10); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable673(Value) values(30); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable673(Value) values(20); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable673(Value) values(10); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable673(Value) values(10); Query OK, 1 row affected (0.28 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable673;
This will produce the following output −
+----+-------+ | Id | Value | +----+-------+ | 1 | 10 | | 2 | 20 | | 3 | 10 | | 4 | 30 | | 5 | 20 | | 6 | 10 | | 7 | 10 | +----+-------+ 7 rows in set (0.00 sec)
Following is the query to display only the values having multiple occurrences −
mysql> select Id,count(*) as myValue from DemoTable673 group by Value having count(*) > 1;
This will produce the following output −
+----+---------+ | Id | myValue | +----+---------+ | 1 | 4 | | 2 | 2 | +----+---------+ 2 rows in set (0.00 sec)
- Related Articles
- Select multiple sums with MySQL query and display them in separate columns?
- MySQL query to select a random row value (Id and Name) having multiple occurrences (Name)?
- MySQL query to display only the column values with corresponding column having whitespace
- MySQL query to group results by date and display the count of duplicate values?
- MySQL query to set values for NULL occurrence
- MySQL Select Multiple VALUES?
- MySQL query to select multiple rows effectively?
- MySQL query to find duplicate tuples and display the count?
- MySQL query to count the duplicate ID values and display the result in a separate column
- MySQL select query with multiple WHERE?
- MySQL query to count occurrences of distinct values and display the result in a new column?
- Select query to display duplicate values with max date
- MySQL query to display ranks of multiple columns?
- MySQL query to fetch multiple least values?
- MySQL query to get the count of values and display the count in a new column ordered in descending order\n

Advertisements