简体   繁体   English

如何仅为特定列选择具有多个匹配的行?

[英]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的表有两列: taggertaggee 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.

相关问题 在MySQL中,只有该列中的所有行都不为null时,才能选择该列 - In MySQL, how can I SELECT a column ONLY if all rows in that column are not null 我如何 select 多行,其中一行在列中具有特定值? - How do I select multiple rows where one row has a specific value in a column? SSRS - 数据集返回多行(有意)-> 如何检查所有行中的特定列的特定值? - SSRS - Dataset return multiple rows (intentionally) -> How can I check a specific column in all rows for a certain value? 我怎样才能 select 多行与同一个表中的其他行的计数在一个单独的列中具有它们的主键? - How can I select multiple rows with the count of other rows in the same table that has their primary keys in a separate column? 如何显示具有多个匹配项的行? - How to display rows with multiple hits? 如何在特定列上不匹配的 select 行(sqlite) - How do I select rows that are not matched on specific column (sqlite) SQL-如何将数据“仅”插入到添加到现有表(多行)的新列中? - SQL - How can I insert data “only” to the new column I added to an existing table (multiple rows)? 如何使用单个列值的多个子字符串选择 SQL 表中的行? - How can I select rows in an SQL table by using multiple substrings of a single column value? 如何限制我的sql select只返回max(date)的一行,而不返回多行? - How can I limit my sql select to return only one row for max(date), and not multiple rows? 如何选择总和(列)的所有列行 - How can I select all my column rows with the sum(column)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM