Answer the question
In order to leave comments, you need to log in
MySQL. Remove duplicate lines?
There is a table with 100.000 lines.
It is necessary to remove duplicate rows (namely rows and not cells)
, that is, so that from the table:
c1 c2
1 2
1 0
4 2
1 0
1 1
2 1
Remaining:
c1 c2
1 2
1 0
4 2
1 1
2 1
One line (1 0 ) needs to be deleted since it was 2.
Can this be done with a request or is it impossible to do without php?
Thank you.
Answer the question
In order to leave comments, you need to log in
CREATE TEMPORARY TABLE tmp_tab AS SELECT DISTINCT * FROM your_table;
DELETE FROM your_table;
INSERT INTO your_table SELECT * FROM tmp_tab;
DROP TABLE tmp_tab;
This one helped me:
ALTER IGNORE TABLE table1 ADD UNIQUE KEY(Name2, Name3);
Add primary key (id) and then:
DELETE FROM table as me, table as clone WHERE me.c1 = clone.c1 AND me.c2 = clone.c2 AND me.id <
clone.id task.
I like this way:
Create a dst table with the same structure but add a numeric field DupCount
query:
INSERT INTO dst SELECT * FROM src ON DUPLICATE KEY UPDATE dst.DupCount = dst.DupCount +1
DupCount for each row will contain the number of duplicates encountered .
You can write a script that will create a dst table, copy the data there, delete the original table and rename dst to src.
I can say that DISCINCT is really a mistake of all databases and it is always necessary to look at it, but in such a simple but not standard task, this is the best option.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question