【发布时间】: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
我有一个表 email 表,它根据 ind_ref(Unique) 记录 email_id。
我只想返回那些共享相同电子邮件 ID 的 IND_ref。在我的示例中,查询应返回 IND_ref 1212 和 1555,因为它们都共享 abc@yahoo.com。我怎样才能做到这一点?任何帮助将不胜感激。
【问题讨论】:
标签: sql sql-server tsql duplicates
你可以使用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
. . .
【讨论】:
SELECT * FROM... 和查询结果集是基本的 SQL 概念。您应该在线学习众多 SQL 教程之一来了解它们。有很多可供选择。
select STAR from StackOverflow,@Yogesh 在结果集中。
t 是表别名,是在查询中引用对象的一种简短方式。查询中的每个表都应该有一个有意义的短别名,然后查询中任何位置的每个列引用(select 列表、join 条件、where 子句等)都应该包含别名,以明确哪个表的每一列都来自。不是要求,而是最佳实践。
使用聚合
select distinct ind_ref
from table_name where email in ( select email from table_name
group by email
having count( *)>1
)
【讨论】: