查找客户表中相关的电子邮件和电话的最佳实践



假设我有以下客户表:

<表类> 行 邮件 手机 组 tbody><<tr>1Dan@gmail.com+ 13569888744512Danny@gmail.com+ 1456988345492

首先,定义如果多行实际上属于同一组,则以最小的组为准。然后,我们只需要循环更新,直到没有发现组减少:

declare @rc bigint=1 -- to go through first iteration check
declare @counter_safety_max=100
declare @counter=1
while(@rc>0 and @counter<@counter_safety_max)
begin
set @counter+=1

update current
set group=lowest.group
from
customer current
cross apply
(   
select top 1 group
from customer cl
where (cl.phone=current.phone or cl.email=current.email) and cl.group<current.group
order by group asc
) as lowest

set @rc=@@rowcount

end

最新更新