M
M
mihavxc2010-10-27 21:29:56
MySQL
mihavxc, 2010-10-27 21:29:56

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

8 answer(s)
B
bigbaraboom, 2010-10-27
@mihavxc

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;

@
@mgyk, 2010-10-28
_

You can also ALTER IGNORE TABLE bzzz add unique index tmp(column1, column2)

D
Dmi3yy, 2013-05-30
@Dmi3yy

This one helped me:
ALTER IGNORE TABLE table1 ADD UNIQUE KEY(Name2, Name3);

Z
zoommy, 2011-09-18
@zoommy

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.

A
Ajex, 2010-10-28
@Ajex

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.

N
NeX, 2010-10-27
@NeX

www.administrating.ru/udalenie-povtoryayushhixsya-strok-v-tablicax-mysql/

B
bigbaraboom, 2010-10-27
@bigbaraboom

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.

P
pvasili, 2011-09-18
@pvasili

Create a composite primary (or simply unique) index on c1 and c2 fields.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question