我有一个MySQL TABLE。它包含我们从数据Feed获取的邮寄地址。但是邮件地址没有客户记录,所以我没有一种简单的方法来匹配客户记录作为关键,以查看它是否已存在于主表中。所以我决定将新的每日数据源添加到主表中,然后删除重复项。
删除重复项最安全的方法是什么?显然,我想忽略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)
以上是否可以安全地防止重复记录被插入以开始?
谢谢!
答案 0 :(得分:0)
这是您可以使用的最简单的查询 根据您的要求选择最大或最小。
DELETE
FROM MyTable
WHERE ID NOT IN
(
SELECT MAX(ID)
FROM MyTable
GROUP BY DuplicateColumn1, DuplicateColumn2, DuplicateColumn3)
谢谢
答案 1 :(得分:-1)
删除FROM测试a LEFT JOIN ( SELECT MIN(id)AS id,company_name,contact_name,address1,address2,address3,city,state,zipcode,phone_number,email_address 从测试 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 IS NULL