SQL查询相似性



我必须显示所有被推荐人引用的客户,其姓氏与客户相同。

您可以将self-join用作

select c1.customer#, c1.lastname, c1.city, c1.zip, c1.referred 
  from customers c1 
  join customers c2 
    on c1.customer# = c2.referred
   and c1.lastname = c2.lastname;
customer#   lastname    city       zip     referred
---------   --------  -----------  ------  ---------
1003        SMITH     TALLAHASSEE   32306   NULL

rextester demo

您可以尝试此查询

select cust.*, cust_ref.*
from customers cust,
referred cust_ref
where cust_ref.lastname = cust.lastname

注意:您可以根据需要选择字段。

我希望它将使用。

最新更新