过滤掉 MySQL 中的唯一行

发布于 2024-10-16 07:01:59 字数 667 浏览 5 评论 0原文

因此,我有大量的 SQL 数据,基本上如下所示:

user | src | dst
1    | 1   | 1
1    | 1   | 1
1    | 1   | 2
1    | 1   | 2
2    | 1   | 1
2    | 1   | 3

我想过滤掉对一个用户唯一的 (src,dst) 对(即使该用户有重复项) ,仅留下属于多个用户的那些对:

user | src | dst
1    | 1   | 1
1    | 1   | 1
2    | 1   | 1

换句话说,对 (1,2) 对用户 1 是唯一的,对 (1,3) 对用户 2 是唯一的,因此它们被删除,只留下所有实例对 (1,1)。

编辑:澄清一下,我对被过滤掉的对不感兴趣,我需要所有唯一的行。

有什么想法吗?下面问题的答案可以找到非唯一对,但我的 SQL-fu 不足以处理要求它们也属于多个用户的复杂性。

如何选择非“唯一”行

So I've got a large amount of SQL data that looks basically like this:

user | src | dst
1    | 1   | 1
1    | 1   | 1
1    | 1   | 2
1    | 1   | 2
2    | 1   | 1
2    | 1   | 3

I want to filter out pairs of (src,dst) that are unique to one user (even if that user has duplicates), leaving behind only those pairs belonging to more than one user:

user | src | dst
1    | 1   | 1
1    | 1   | 1
2    | 1   | 1

In other words, pair (1,2) is unique to user 1 and pair (1,3) to user 2, so they're dropped, leaving behind only all instances of pair (1,1).

Edit: To clarify, I am not interested in the pairs that get filtered out, I need all the rows where the pairs are not unique.

Any ideas? The answers to the question below can find the non-unique pairs, but my SQL-fu doesn't suffice to handle the complication of requiring that they belong to multiple users as well.

How to select non "unique" rows

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

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

发布评论

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

评论(3

睡美人的小仙女 2024-10-23 07:01:59

我的解决方案将是这样的(经过测试):

SELECT user, src, dst, COUNT(user) as num_of_users
FROM test
GROUP BY src, dst
HAVING num_of_users = 1

编辑:
以下代码生成您在示例中提供的结果。

SELECT test.user, test.src, test.dst
FROM test
INNER JOIN
(
    SELECT user, src, dst, COUNT(DISTINCT user) as num_of_users
    FROM test
    GROUP BY src, dst
    HAVING num_of_users > 1
) as inner_sql
USING(src, dst)

该解决方案是您正在寻找的解决方案吗?表现如何?

My solution would be something like this (tested):

SELECT user, src, dst, COUNT(user) as num_of_users
FROM test
GROUP BY src, dst
HAVING num_of_users = 1

Edit:
The following code produces the results that you provided in your example.

SELECT test.user, test.src, test.dst
FROM test
INNER JOIN
(
    SELECT user, src, dst, COUNT(DISTINCT user) as num_of_users
    FROM test
    GROUP BY src, dst
    HAVING num_of_users > 1
) as inner_sql
USING(src, dst)

Is this solution the one you are looking for? How is the performance?

黄昏下泛黄的笔记 2024-10-23 07:01:59

使用 EXISTS 语法加入

SELECT t1.*
FROM   table t1
WHERE EXISTS (SELECT 1 
              FROM   table t2 
              WHERE  t1.src   = t2.src 
               AND   t1.dst   = t2.dst
               AND   t1.user != t2.user)

另一个可能有效的选项(未测试)。如果它有效的话,效率会更高:

SELECT user, src, dst
FROM   table
GROUP BY src, dst
HAVING COUNT(DISTINCT user) > 1

Join using EXISTS syntax

SELECT t1.*
FROM   table t1
WHERE EXISTS (SELECT 1 
              FROM   table t2 
              WHERE  t1.src   = t2.src 
               AND   t1.dst   = t2.dst
               AND   t1.user != t2.user)

Another option that MIGHT work (didn't test). If it works it would be more efficient:

SELECT user, src, dst
FROM   table
GROUP BY src, dst
HAVING COUNT(DISTINCT user) > 1
很糊涂小朋友 2024-10-23 07:01:59

可能和一些团体在一起?

类似的东西

select user,src,dst,count(*) as c from table group by user,src,dst 
having c = 1

May be with some group by?

something like

select user,src,dst,count(*) as c from table group by user,src,dst 
having c = 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文