MySQL 如何在表中查找重复值
假设我们有一个名为 stock_item 的表,其中列 quantity 具有重复值,即对于项目名称“ Notebooks ”和“ Pencil ”,列“ Quantity” 具有相同的“ 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)
现在,使用以下查询语句,我们可以找到列“quantity”中的重复值以及这些项的名称。
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)
阅读更多:MySQL 教程