0

我有一个 MySQL 表。它包含我们从数据馈送中获得的邮寄地址。但是邮寄地址没有客户记录,所以我没有一种简单的方法来匹配客户记录作为键来查看它是否已经存在于主表中。所以我决定将新的每日数据馈送添加到主表中,然后删除重复项。

删除重复项最安全的方法是什么?显然,我想忽略 ID 列字段。但是我如何为以下字段执行此操作:

company_name
contact_name
address1
address2
address3
city
state
zipcode
phone_number
email_address

如果我重建 MySQL TABLE 以包含带有 UNIQUE KEY 的 ALTER TABLE,那会安全吗?例如:

ALTER TABLE people ADD UNIQUE KEY (company_name,contact_name,address1,address2,address3,city,state,zipcode,phone_number,email_address)

上述内容是否可以安全地防止重复记录被插入?

谢谢!

4

2 回答 2

0

这是您可以使用的最简单的查询,根据您的要求选择最大值或最小值。

DELETE
FROM MyTable
WHERE ID NOT IN
(
SELECT MAX(ID)
FROM MyTable
GROUP BY DuplicateColumn1, DuplicateColumn2, DuplicateColumn3)

谢谢

于 2013-05-06T09:12:23.723 回答
-1

DELETE a FROM test a LEFT JOIN ( SELECT MIN(id) AS id, company_name, contact_name, address1, address2, address3, city, state, zipcode, phone_number, email_address FROM test GROUP BY company_name, contact_name, address1, address2, address3, city , state, zipcode, phone_number, email_address ) b ON a.id = b.id AND a.company_name = b.company_name AND a.contact_name = b.contact_name AND a.address1 = b.address1 AND a.address2 = b.address2 AND a.address3 = b.address3 AND a.city = b.city AND a.state = b.state AND a.zipcode = b.zipcode AND a.phone_number = b.phone_number AND a.email_address = b.email_address WHERE b .id 为空

于 2013-10-22T11:57:09.283 回答