SQL 查询仅查找 2 列中的唯一字符串

发布于 2024-10-18 15:11:26 字数 256 浏览 4 评论 0原文

我有一个包含 2 列的表,两列都有名称

,我想要一个仅查找唯一名称的查询 - 在整个表中最多只出现一次的名称。

例如,对于下表:

NAME1    |     NAME2
--------------------
DAN             MIKE
MIKE            TONY
FOO              DAN

它应该只返回 FOO 和 TONY。

谢谢

I have a table with 2 columns, both having names

I want a query that finds only unique names - names that only appear once max in the entire table.

For example, for the following table:

NAME1    |     NAME2
--------------------
DAN             MIKE
MIKE            TONY
FOO              DAN

It should only return FOO and TONY.

Thanks

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

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

发布评论

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

评论(4

时光与爱终年不遇 2024-10-25 15:11:26

我想你可以通过几种方法来做到这一点,但这是一种(假设 Name1 和 Name2 具有相同的或隐式可转换的类型):

SELECT [Name]
FROM (SELECT Name1 AS [Name] FROM [Table] UNION ALL SELECT Name2 FROM [Table])
GROUP BY [Name]
HAVING COUNT(*) = 1

I guess you could do it a few ways, but here's one (assuming Name1 and Name2 are of the same, or implicitly convertible types):

SELECT [Name]
FROM (SELECT Name1 AS [Name] FROM [Table] UNION ALL SELECT Name2 FROM [Table])
GROUP BY [Name]
HAVING COUNT(*) = 1
冰之心 2024-10-25 15:11:26

简单的怎么样:

select name1 from table where name1 not in (select distinct name2 from table ) 
union 
select name2 from table where name2 not in (select distinct name1 from table );

How about the simple:

select name1 from table where name1 not in (select distinct name2 from table ) 
union 
select name2 from table where name2 not in (select distinct name1 from table );
毁虫ゝ 2024-10-25 15:11:26

只要 name1name2 不能为 null,就使用此查询:

SELECT t.Name1
FROM tableName t LEFT JOIN tableName t2 ON t.NAME1 = t2.NAME2
WHERE t2.NAME2 IS NULL
GROUP BY t.Name1
HAVING COUNT(*) = 1
UNION ALL
SELECT t.Name2
FROM tableName t LEFT JOIN tableName t2 ON t.NAME2 = t2.NAME1
WHERE t2.NAME1 IS NULL
GROUP BY t.Name2
HAVING COUNT(*) = 1

如果它们可以为 null,则使用此查询:

SELECT Name
FROM (
  SELECT t.Name1 Name
  FROM tableName
  GROUP BY t.Name1
  HAVING COUNT(*) = 1
  UNION ALL
  SELECT t.Name2 Name
  FROM tableName
  GROUP BY t.Name2
  HAVING COUNT(*) = 1
) rs
GROUP BY Name
HAVING COUNT(*) = 1

As long as name1 and name2 cannot be null, use this query:

SELECT t.Name1
FROM tableName t LEFT JOIN tableName t2 ON t.NAME1 = t2.NAME2
WHERE t2.NAME2 IS NULL
GROUP BY t.Name1
HAVING COUNT(*) = 1
UNION ALL
SELECT t.Name2
FROM tableName t LEFT JOIN tableName t2 ON t.NAME2 = t2.NAME1
WHERE t2.NAME1 IS NULL
GROUP BY t.Name2
HAVING COUNT(*) = 1

If they can be null, use this query:

SELECT Name
FROM (
  SELECT t.Name1 Name
  FROM tableName
  GROUP BY t.Name1
  HAVING COUNT(*) = 1
  UNION ALL
  SELECT t.Name2 Name
  FROM tableName
  GROUP BY t.Name2
  HAVING COUNT(*) = 1
) rs
GROUP BY Name
HAVING COUNT(*) = 1
哭泣的笑容 2024-10-25 15:11:26

@CheeseConQueso 对这个问题的解释与我不同。我认为您的意思是来自两列组合的唯一名称。

这是我的版本:

select name, count(*) from (select name1 as name from table union select name2 as name from table) group by name having count(*) = 1;

您可以将整个内容包装在“从...中选择名称”中以单独获取名称,或者可以使用分析函数重写它,而不是分组以过滤重复项。取决于你的数据库的能力。

@CheeseConQueso interpreted the question differently than I did. I thought you meant unique names from both columns combined.

Here's my version:

select name, count(*) from (select name1 as name from table union select name2 as name from table) group by name having count(*) = 1;

You can wrap the whole thing in a "select name from ..." to get the names alone, or this could be rewritten using an analytic function instead of grouping to filter the duplicates. Depends on the capabilities of your database.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文