假设我们有一个名为“stock_item”的表,其中列数量具有重复值,即对于项目名称“笔记本”和“铅笔”,列“数量”具有重复值“40”,如表中所示表。
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)
现在,借助以下使用 MySQL JOINS 的查询,我们可以找到“数量”列中的重复值以及项目名称。
mysql> Select distinct g.item_name,g.quantity from stock_item g
-> INNER JOIN Stock_item b ON g.quantity = b.quantity
-> WHERE g.item_name<>b.item_name;
+-----------+----------+
| item_name | quantity |
+-----------+----------+
| Pencil | 40 |
| Notebooks | 40 |
| Shoes | 29 |
| Trousers | 29 |
| Shirts | 29 |
+-----------+----------+
5 rows in set (0.00 sec)