first_name

问题描述 投票:2回答:1

我试图在一个类似于这样的表中找到重复的客户。

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

上面的解决方案可能有性能问题

mysql
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.