문제

I am having a table with the following columns

code  (varchar)
desc  (varchar)
attr1 (varchar)
attr2 (varchar)
attr3 (varchar)
reference

And the table contains bulk data.What i need is that i need to remove all entries from the table having duplicate values for code,attr1,attr2,attr3.I tried with adding unique index using these columns but didn't worked.Can i use some script for these.All these columns allows null values.my database engine is myisam.

도움이 되었습니까?

해결책

You can add a unique index and drop the duplicates with

ALTER IGNORE TABLE your_table
ADD UNIQUE INDEX dup_idx (code, attr1, attr2, attr3);

다른 팁

DELETE a 
FROM your_table a 
LEFT JOIN
(
  SELECT MIN(id) AS id, code, attr1, attr2, attr3 
  FROM your_table 
  GROUP BY code, attr1, attr2, attr3
) b 
ON a.id = b.id 
AND a.code = b.code 
AND a.attr1 = b.attr1 
AND a.attr2 = b.attr2 
AND a.attr3 = b.attr3 
WHERE b.id IS NULL
ALTER IGNORE TABLE your_table
ADD UNIQUE INDEX `UniqueData` (`code`, `attr1` , `attr2` ,`attr3`) ;

This should actually work but, it will not bother to delete or change the older data. It will make sure that the future data will be unique.

If you try to add a duplicate entry then it will throw an error.

If you still can't get the solution while using this, share the error message you are getting.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top