How can I select only rows with multiple images for a specific column?

I am not sure how to formulate this question, so I will give an example:

Suppose there is a table with a name taggedthat has two columns: taggerand taggee. What does an SQL query look like to return taggee(s) that are on multiple lines? That is, they were tagged 2 or more times by anyone tagger.

I need a "general" SQL query, not one that works only in a specific DBMS.

EDIT: Added "tagged 2 or more times with any tag ."

+3
source share
2 answers

HAVINGcan work with the result of aggregate functions. Therefore, if you have such data:

Row    tagger  |  taggee
       --------+----------
1.     Joe     |  Cat
2.     Fred    |  Cat
3.     Denise  |  Dog
4.     Joe     |  Horse
5.     Denise  |  Horse

, Cat, .

, , :

SELECT taggee, count(*) FROM tagged GROUP BY taggee HAVING count(*) > 1

, " ", ? 1 Cat, 2?

+9
select distinct t1.taggee from tagged t1 inner join tagged t2 
        on t1.taggee = t2.taggee and t1.tagger != t2.tagger;

,

0

Source: https://habr.com/ru/post/1787248/


All Articles