Remove duplicate record from table
I have a table in MySQL named ZipCode and its fields are: id, zipcode, city, state. It has some duplicate records; some zipcode are showing two times I want to remove all that zipcodes that are coming twice; each zipcode must be stored just once. How can I make 开发者_开发技巧it correct?
-- remove duplicates
DELETE z1
FROM ZipCode z1, ZipCode z2
WHERE z1.zipcode = z2.zipcode AND z2.id < z1.id;
-- add a primary key
ALTER TABLE ZipCode ADD PRIMARY KEY (zipcode);
-- or a unique key
ALTER TABLE ZipCode ADD UNIQUE INDEX zipcode_unique (zipcode);
This set of three queries will remove all duplicates:
CREATE TABLE new_table AS SELECT * FROM old_table WHERE 1 GROUP BY zip;
DROP TABLE old_table;
RENAME TABLE new_table TO old_table;
Try this its working fine
DELETE
FROM
insurance_policy_customers ipc
WHERE
ipc.id NOT IN (SELECT
min(ipc.id)
FROM
insurance_policy_customers ipc
GROUP BY
ipc.resident_insurance_policy_id )
精彩评论