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.

3
  • what would the result be exactly from the example data? how do you know what you would keep? (what happens with the other columns?) Commented Nov 6, 2013 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 Commented Nov 6, 2013 at 17:15
  • try the answer posted here: stackoverflow.com/a/3312098/623952 or here stackoverflow.com/a/4685232/623952 Commented Nov 6, 2013 at 17:53

3 Answers 3

1
ALTER IGNORE TABLE tableName
ADD CONSTRAINT SomeName UNIQUE (name ,attribute_id)
Sign up to request clarification or add additional context in comments.

Comments

1
DELETE 
FROM tbl 
WHERE id IN (
SELECT id from tbl 
GROUP BY name,attribute_id 
HAVING COUNT(*)>2
)

4 Comments

count(*)>=2... would that delete all records of things with more than one occurrence? i think OP wants to save one copy.
Your answer is giving me 1093 error Can't specify target table for update in FROM clause
It WILL delete ALL your duplicates.
you still need to change the count(*)>2 to >=2 otherwise it will only work for things with 3 or more duplicates...
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

1 Comment

mysql doesn't do partition by

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.