 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do we count the records from MySQL table where column holds duplicate/triplicates data?
Suppose we have the following table named stock_item in which the column quantity is having duplicate values i.e. for item name ‘Notebooks’ and ‘Pencil’, the column ‘Quantity’ is having duplicate values ‘40’ and for items ‘Shirts’, ‘Shoes’ and ‘Trousers’ triplicate value 29 is hold by column ‘quantity’ as shown in the table.
mysql> Select * from stock_item; +------------+----------+ | item_name |quantity | +------------+----------+ | Calculator | 89 | | Notebooks | 40 | | Pencil | 40 | | Pens | 32 | | Shirts | 29 | | Shoes | 29 | | Trousers | 29 | +------------+----------+ 7 rows in set (0.00 sec)
Now with the help of following query we can find the total number of duplicate/triplicate values in column ‘quantity’.
mysql> Select COUNT(quantity) AS duplicate_triplicate -> from stock_item -> group by quantity having duplicate_triplicate> 1; +----------------------+ | duplicate_triplicate | +----------------------+ | 3 | | 2 | +----------------------+ 2 rows in set (0.00 sec)
The result above shows that column ‘quantity’ is having a value that is repeated for three times and a value that is repeated for two times.
Advertisements
 