M
M
MasterCopipaster2020-07-31 21:40:47
SQL
MasterCopipaster, 2020-07-31 21:40:47

SQL query to remove duplicates from a table by 3 fields?

Tell me how to remove duplicate values ​​for the fields product_id, category_id, modification_id


id|product_id|category_id|modification_id|
--|----------|-----------|---------------|
1| 333| 133| 17344|
2| 333| 133| 17344|
3| 333| 133| 17344|
4| 333| 133| 17344|
5| 333| 133| 17344|
6| 333| 133| 17344|
7| 333| 133| 17344|
8| 333| 133| 17344|
9| 333| 133| 17344|
10| 333| 133| 17344|
11| 333| 133| 2069|
12| 333| 133| 2069|
13| 338| 387| 18361|
14| 338| 387| 18361|
15| 333| 133| 17344|
16| 333| 133| 17344|
17| 333| 133| 17344|
18| 333| 133| 17344|
19| 333| 133| 17344|
20| 333| 133| 17344|


It should turn out like this


id|product_id|category_id|modification_id|
--|----------|-----------|---------------|
1| 333| 133| 17344|
11| 333| 133| 2069|
13| 338| 387| 18361|
15| 333| 133| 17344|

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2020-07-31
@MasterCopipaster

More or less like this:

DELETE t1 FROM tbl_name t1
INNER JOIN tbl_name t2 
WHERE 
    t1.id < t2.id AND 
    t1.product_id = t2.product_id AND
    t1.category_id = t2.category_id AND
    t1.modification_id = t2.modification_id;

https://www.mysqltutorial.org/mysql-delete-duplica...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question