过滤掉 MySQL 中的唯一行
因此,我有大量的 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我的解决方案将是这样的(经过测试):
编辑:
以下代码生成您在示例中提供的结果。
该解决方案是您正在寻找的解决方案吗?表现如何?
My solution would be something like this (tested):
Edit:
The following code produces the results that you provided in your example.
Is this solution the one you are looking for? How is the performance?
使用 EXISTS 语法加入
另一个可能有效的选项(未测试)。如果它有效的话,效率会更高:
Join using EXISTS syntax
Another option that MIGHT work (didn't test). If it works it would be more efficient:
可能和一些团体在一起?
类似的东西
May be with some group by?
something like