1

I've got table with following columns:

id,name,attribute_id,params_desc

The data in table looks like this:

0,'some',1,'something'
1,'some',2,'somethingelse'
2,'some',3,'somethingelses'
3,'some',1,'something'

What I need is to remove duplicates, which have the same name and attribute_id.

I was unable to find some working solution here, as DISTINCT or UNIQUE INDEX or INSERT IGNORE did not work for me.

Thx for your time.

JTC
  • 3,344
  • 3
  • 28
  • 46
  • what would the result be exactly from the example data? how do you know what you would keep? (what happens with the other columns?) – gloomy.penguin Nov 06 '13 at 17:07
  • I just want to save one copy, only the id is different and i dont care about that, in this example, remove last row – JTC Nov 06 '13 at 17:15
  • try the answer posted here: http://stackoverflow.com/a/3312098/623952 or here http://stackoverflow.com/a/4685232/623952 – gloomy.penguin Nov 06 '13 at 17:53

3 Answers3

1
ALTER IGNORE TABLE tableName
ADD CONSTRAINT SomeName UNIQUE (name ,attribute_id)
Mihai
  • 26,325
  • 7
  • 66
  • 81
1
DELETE 
FROM tbl 
WHERE id IN (
SELECT id from tbl 
GROUP BY name,attribute_id 
HAVING COUNT(*)>2
)
Arun Killu
  • 13,581
  • 5
  • 34
  • 61
0

Apologies since I'm not sure the syntax is the same in MySQL as in SQLServer - however a quick google suggests it might be, otherwise perhaps this will point you in the right direction:

DELETE a FROM 
(
    SELECT id
            ,name
            ,attribute_id
            ,params_desc
            ,row = ROW_NUMBER() OVER (PARTITION BY name, attribute_id ORDER BY id ASC)
    FROM Table
) a
WHERE row > 1
OGHaza
  • 4,795
  • 7
  • 23
  • 29