Steven Matthews
Steven Matthews

Reputation: 11325

Removing duplicates MySQL database

I'm trying to remove duplicates from a MySQL table where two column values will be the same.

In this case, I want to, say, have an id column (called nid), and a hash column with the same values:

| nid |    hash    |
|  2  |   932298   |
|  2  |   932298   |

I'd like only one of them to survive, preferably the first one inserted in the database.

I'm looking at this post but my use case is slightly different:

MySQL remove duplicates from big database quick

I'm also open to other options

Upvotes: 0

Views: 114

Answers (2)

avisheks
avisheks

Reputation: 1180

simplest way of achieving it I believe:

1) create table `table_copy`(id int primary key,hash varchar(255), unique(`hash`)) select distinct id,hash from `table_name`;
2) drop table `table_name`;
3) rename table `table_copy` to `table_name`;

Upvotes: 0

Jerko W. Tisler
Jerko W. Tisler

Reputation: 989

ALTER IGNORE TABLE `table_name` ADD UNIQUE (`hash`)

Upvotes: 1

Related Questions