[英]How can I select only rows with multiple hits for a specific column?
I am not sure how to phrase this question so I'll give an example: 我不知道该怎么说这个问题,所以我举一个例子:
Suppose there is a table called tagged
that has two columns: tagger
and taggee
. 假设有一个名为
tagged
的表有两列: tagger
和taggee
。 What would the SQL query look like to return the taggee
(s) that are in multiple rows? SQL查询返回多行中的
taggee
? That is to say, they have been tagged 2 or more times by any tagger
. 也就是说,它们被任何
tagger
标记了2次或更多次。
I would like a 'generic' SQL query and not something that only works on a specific DBMS. 我想要一个'通用'SQL查询,而不是只适用于特定DBMS的东西。
EDIT: Added "tagged 2 or more times by any tagger." 编辑:添加“由任何标记器标记2次或更多次。”
HAVING
can operate on the result of aggregate functions. HAVING
可以对聚合函数的结果进行操作。 So if you have data like this: 所以如果你有这样的数据:
Row tagger | taggee
--------+----------
1. Joe | Cat
2. Fred | Cat
3. Denise | Dog
4. Joe | Horse
5. Denise | Horse
It sounds like you want Cat , Horse . 听起来你想要猫 , 马 。
To get the taggee's that are in multiple rows, you would execute: 要获取多行的标记,您可以执行:
SELECT taggee, count(*) FROM tagged GROUP BY taggee HAVING count(*) > 1
That being said, when you say "select only rows with multiple hits for a specific column", which row do you want? 话虽这么说,当你说“只选择具有特定列的多次点击的行”时,你想要哪一行 ? Do you want row 1 for Cat, or row 2 ?
你想要第1行是Cat还是第2行?
select distinct t1.taggee from tagged t1 inner join tagged t2
on t1.taggee = t2.taggee and t1.tagger != t2.tagger;
Will give you all the taggees who have been tagged by more than one tagger 将为您提供已被多个标记器标记的所有标记
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.