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);
質問
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.