假设我们有一个名为“ 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)