Mysql 搜索具有相似功能的重复客户

Mysql 搜索具有相似功能的重复客户,mysql,Mysql,我试图在如下表中找到重复的客户: customer_id | first_name | last_name ------------------------------------- 0 | Rich | Smith 1 | Paul | Jones 2 | Richard | Smith 3 | Jimmy | Roberts 在这种情况下,我需要一个查询,该查询将返回c

我试图在如下表中找到重复的客户:

customer_id | first_name | last_name 
-------------------------------------
          0 | Rich       | Smith
          1 | Paul       | Jones
          2 | Richard    | Smith
          3 | Jimmy      | Roberts
在这种情况下,我需要一个查询,该查询将返回customer_id 0和customer_id 2。查询需要找到客户可能缩短了姓名的匹配项,Rich而不是Richard,Rob而不是Robert

我有这个查询,但它只返回一个匹配项,而不是两个匹配项。我需要通过查询返回Rich和Richard

select distinct customers.customer_id, concat(customers.first_name,' ',customers.last_name) as name from customers
inner join customers dup on customers.last_name = dup.last_name
where (dup.first_name like concat('%', customers.first_name, '%')
and dup.customer_id <> customers.customer_id )
order by name
有人能给我指一下正确的方向吗

根据@tsOverflow,这是解决我的问题的最后一个查询:

select distinct customers.customer_id, concat(customers.first_name,' ',customers.last_name) as name 
from customers
    inner join customers dup on customers.last_name = dup.last_name
where ((dup.first_name like concat('%', customers.first_name, '%') 
            OR (customers.first_name like concat('%', dup.first_name, '%')) 
        )
    and dup.customer_id <> customers.customer_id )
order by name

上述解决方案可能存在性能问题

你的问题是因为Rich是Richard的子串,而不是相反

这将检查两种方式:

select distinct randomtest.customer_id, concat(randomtest.first_name,' ',randomtest.last_name) as name 
from randomtest
    inner join randomtest dup on randomtest.last_name = dup.last_name
where ((dup.first_name like concat('%', randomtest.first_name, '%') 
            OR (randomtest.first_name like concat('%', dup.first_name, '%')) 
        )
    and dup.customer_id <> randomtest.customer_id )
order by name
我添加了OR,并以另一种方式执行类似的检查。 请注意,在查询中使用like语句会带来性能方面的问题——我不是这方面的专家,只是一个想法

编辑:
正如其他人在评论中提到的,这只适用于缩短版本实际上只是一个子字符串的情况,而不适用于Michael->Mike或William->Bill,另一方面,约翰和一个叫约翰逊的家伙也可能是完全不同的两个人。

你认为MySQL如何将“Richard”这个名字改为“Rich”?你需要辅助信息来解决这个问题。根据输入信息的人,你可以得到:Mike=Mike或Mike=Michael and Jo=Joseph and Jo=Joann,同样的注释是:为什么Rich Smith=Richard Smith?就是这样。为了子孙后代,我将把最后一个问题添加到上述问题中。谢谢你的帮助!这让我很困惑。你应该把这个查询作为更新添加到你原来的帖子中,这样更容易阅读@tsOverflow:不仅如此,OR也会影响性能。根据您的编辑-在本例中,这是最好的。用户将看到一个可能重复的列表,并可以在比较两者后选择合并客户数据。这是一个独特的情况,所以这个解决方案可能不适合其他人的需要,但它正是我所需要的。再次感谢!