Home > Net >  Getting all rows where a key occurs only once among all columns
Getting all rows where a key occurs only once among all columns

Time:09-29

column1 column2 
A       1
A       2
A       3
B       1
C       1
C       2
D       1

I want a query that prints

column1 column2 
B       1
D       1

Since B and D occur just once in the table.

I have tried queries like select column1 from table group by column1 having count(column1)=1 but this didn't work.

CodePudding user response:

SELECT column1
FROM TABLE
GROUP BY column1
HAVING count(column1)=1

Hope this works in your case

CodePudding user response:

Maybe you need in this:

SELECT t1.*
FROM table t1
JOIN (
    SELECT column1
    FROM table t2
    GROUP BY column1
    HAVING COUNT(column1)=1
    ) t3 USING (column1)
  • Related