【问题标题】:SQL Query to find Duplicate email under different ID [duplicate]SQL查询以查找不同ID下的重复电子邮件[重复]
【发布时间】:2019-07-23 14:59:00
【问题描述】:

我有一个表 email 表,它根据 ind_ref(Unique) 记录 email_id。

我只想返回那些共享相同电子邮件 ID 的 IND_ref。在我的示例中,查询应返回 IND_ref 1212 和 1555,因为它们都共享 abc@yahoo.com。我怎样才能做到这一点?任何帮助将不胜感激。

【问题讨论】:

    标签: sql sql-server tsql duplicates


    【解决方案1】:

    你可以使用exists

    select t.*
    from table t
    where exists (select 1 from table t1 where t1.email = t.email and t1.ind_ref <> t.ind_ref);
    

    t.* 表示您正在从table 中选择所有列,如果您只想要有限的列,那么您可以这样做:

    select t.ind_ref, t.email
    . . . 
    

    【讨论】:

    • 你是明星。非常感谢。我可以知道您为什么在选择查询中使用 t.* 以及为什么使用它。我是 SQL 新手,发现它很新。您是否正在创建虚拟临时表而不实际创建一个?请解释您的查询
    • @Biswa、SELECT * FROM... 和查询结果集是基本的 SQL 概念。您应该在线学习众多 SQL 教程之一来了解它们。有很多可供选择。
    • 虽然,公平地说,如果你select STAR from StackOverflow,@Yogesh 在结果集中。
    • @EricBrandt 我知道 Select * from table t 但从未遇到 select t.* from table t 那 (.*) 意味着我没有得到什么。
    • @Biswa,t 是表别名,是在查询中引用对象的一种简短方式。查询中的每个表都应该有一个有意义的短别名,然后查询中任何位置的每个列引用(select 列表、join 条件、where 子句等)都应该包含别名,以明确哪个表的每一列都来自。不是要求,而是最佳实践。
    【解决方案2】:

    使用聚合

      select distinct ind_ref
     from table_name where email in (  select email from table_name
        group by email 
        having count( *)>1
       )
    

    【讨论】:

      猜你喜欢
      • 2015-01-24
      • 1970-01-01
      • 2013-09-06
      • 2015-08-18
      • 2020-03-31
      • 1970-01-01
      • 2020-01-23
      • 1970-01-01
      • 2013-03-20
      相关资源
      最近更新 更多