1

I'm after some help putting together a query to remove duplicate data in a specific field in one of my tables.

I have a table called contacts that I need to import in to another system. The new system requires that the email field be unique. I need a query that will allow me to search the email field and remove any duplicate data or set it to "".

I don't want to remove the rows, just the duplicate of the email. So if there are two records contain the email gaz@example.com, then I want to keep the first reference while removing the second.

Seems like this should be a simple thing to do but I'm struggling working our how to achieve it. Thanks.

4

2 回答 2

3

您需要使用类似于以下内容的查询:

UPDATE CONTACTS A, CONTACTS B
  SET B.EMAIL=NULL
WHERE A.EMAIL=B.EMAIL
  AND A.KEY_FIELD>B.KEY_FIELD

使用字段引用来确定要删除的内容。

于 2013-08-06T17:57:49.107 回答
0
UPDATE CONTACTS SET b.email = ""
FROM CONTACTS a, CONTACTS b
WHERE a.EMAIL = b.EMAIL AND a.ID>b.ID

另一种可能的解决方案

参考 :

http://stackoverflow.com/questions/2044467/how-to-update-two-tables-in-one-statement-in-sql-server-2005
于 2013-08-06T20:01:20.567 回答