我们如何在 MySQL 表中查找重复值?
mysqlmysqli database更新于 2023/11/12 4:57:00
假设我们有以下名为 stock_item 的表,其中数量列具有重复值,即对于项目名称"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)