SQL上的Soundex模糊匹配 - 表的组合

时间:2016-07-27 08:49:55

标签: sql

方案: 我们有一个名为AddressTable的表。 在那里我们有4列: AddressID int PK不允许空值 AddressLine1 nvarchar(255) AddressLine2 nvarchar(255) AddressLine3 nvarchar(255)

我应该使用什么样的逻辑来比较使用模糊匹配相关函数的所有记录来返回可能重复的ID?

select AddressID, SOUNDEX(AddressLine1, AddressLine2, AddressLine3) from [AddressTable]

1 个答案:

答案 0 :(得分:0)

自我加入:

select distinct t1.*
from [AddressTable] t1
join [AddressTable] t2
  on t1.AddressID <> t2.AddressID and     
     SOUNDEX(t1.AddressLine1, t1.AddressLine2, t1.AddressLine3) =
     SOUNDEX(t2.AddressLine1, t2.AddressLine2, t2.AddressLine3)

或者,执行EXISTS

select t1.*
from [AddressTable] t1
where exists (select * from [AddressTable] t2
              where t1.AddressID <> t2.AddressID
                and SOUNDEX(t1.AddressLine1, t1.AddressLine2, t1.AddressLine3) =
                    SOUNDEX(t2.AddressLine1, t2.AddressLine2, t2.AddressLine3))