How do we count the records from MySQL table where column holds duplicate/triplicates data?

MySQLMySQLi Database

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.

raja
Published on 21-Feb-2018 12:44:06
Advertisements