Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> MySQL

เราจะค้นหาค่าที่ซ้ำกันที่มีอยู่ในตาราง MySQL ได้อย่างไร


สมมติว่าเรามีตารางต่อไปนี้ชื่อ 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> 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)