T-SQL 如何:比较并列出表中的重复条目

发布于 2024-09-04 17:16:18 字数 636 浏览 1 评论 0原文

SQL Server 2000。单个表有一个用户列表,其中包括唯一的用户 ID 和非唯一的用户名。

我想搜索表并列出共享相同非唯一用户名的所有用户。例如,我的表如下所示:

ID   User Name   Name
==   =========   ====
0    parker      Peter Parker
1    parker      Mary Jane Parker      
2    heroman     Joseph (Joey) Carter Jones
3    thehulk     Bruce Banner

我想要做的是执行 SELECT 并将结果集设置为:

ID   User Name   Name
==   =========   ====
0    parker      Peter Parker
1    parker      Mary Jane Parker   

来自我的表。

我不是 T-SQL 专家。我可以执行基本的连接等操作,但我认为必须有一种优雅的方法来执行此操作。除了优雅之外,一定有任何方法可以做到这一点。

我很感谢您可以在这个主题上帮助我的任何方法。谢谢! - -担 - -

SQL Server 2000. Single table has a list of users that includes a unique user ID and a non-unique user name.

I want to search the table and list out any users that share the same non-unique user name. For example, my table looks like this:

ID   User Name   Name
==   =========   ====
0    parker      Peter Parker
1    parker      Mary Jane Parker      
2    heroman     Joseph (Joey) Carter Jones
3    thehulk     Bruce Banner

What I want to do is do a SELECT and have the result set be:

ID   User Name   Name
==   =========   ====
0    parker      Peter Parker
1    parker      Mary Jane Parker   

from my table.

I'm not a T-SQL guru. I can do the basic joins and such, but I'm thinking there must be an elegant way of doing this. Barring elegance, there must be ANY way of doing this.

I appreciate any methods that you can help me with on this topic. Thanks!
---Dan---

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

吃素的狼 2024-09-11 17:16:18

单程

select t1.* from Table t1
join(
select username from Table
group by username
having count(username) >1) t2 on t1.username = t2.username

One way

select t1.* from Table t1
join(
select username from Table
group by username
having count(username) >1) t2 on t1.username = t2.username
夜雨飘雪 2024-09-11 17:16:18

我能想到的最简单的方法是使用子查询:

select * from username un1 where exists
(select null from username un2 
where un1.user_name = un2.user_name and un1.id <> un2.id);

The simplest way I can think of to do this uses a sub-query:

select * from username un1 where exists
(select null from username un2 
where un1.user_name = un2.user_name and un1.id <> un2.id);
往昔成烟 2024-09-11 17:16:18

子查询选择具有该名称的 > 1 行的所有名称...外部查询选择与这些 ID 匹配的所有行。

SELECT T.* 
FROM T
    , (SELECT   Dupe_candidates.USERNAME
       FROM     T AS Dupe_candidates
       GROUP BY Dupe_candidates.USERNAME
       HAVING   count(*)>1
     ) Dupes
WHERE T.USERNAME=Dupes.USERNAME

The sub-query selects all names that have >1 row with that name... outer query selects all the rows matching those IDs.

SELECT T.* 
FROM T
    , (SELECT   Dupe_candidates.USERNAME
       FROM     T AS Dupe_candidates
       GROUP BY Dupe_candidates.USERNAME
       HAVING   count(*)>1
     ) Dupes
WHERE T.USERNAME=Dupes.USERNAME
凯凯我们等你回来 2024-09-11 17:16:18

您可以尝试以下操作:

SELECT * 
FROM dbo.Person as p1 
WHERE 
(SELECT COUNT(*) FROM dbo.Person AS p2 WHERE p2.UserName = p1.UserName) > 1;

You can try the following:

SELECT * 
FROM dbo.Person as p1 
WHERE 
(SELECT COUNT(*) FROM dbo.Person AS p2 WHERE p2.UserName = p1.UserName) > 1;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文