如何使用 JOINS 查找 MySQL 表中的重复值?

mysqlmysqli database更新于 2025/5/28 15:08:17

假设我们有以下名为"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)

现在,使用 MySQL JOINS 的以下查询,我们可以找到列‘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)

相关文章


有用资源