Home > Database > Mysql Tutorial > How can we find the duplicate values ​​available in a MySQL table using JOINS?

How can we find the duplicate values ​​available in a MySQL table using JOINS?

王林
Release: 2023-09-24 21:37:02
forward
1836 people have browsed it

我们如何使用 JOINS 找到 MySQL 表中可用的重复值?

Suppose we have a table named "stock_item" in which the column quantity has duplicate values, i.e. for the item names "notebook" and "pencil", the column "quantity" has duplicate values "40", as shown in the table.

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)
Copy after login

Now, with the help of the following query using MySQL JOINS, we can find the duplicate values ​​in the "Quantity" column along with the item name.

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)
Copy after login

The above is the detailed content of How can we find the duplicate values ​​available in a MySQL table using JOINS?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template