Java
Java

Reputation: 1282

Select multiple rows with condition on each columns

I am sure this could be very silly question, but I was not able to find answer from the internet.

I am trying to select rows with this conditon.

Select all rows if Side has both 'Right' and 'Left'.

enter image description here

In this case, it will select both row of Id 3 and 4 (yellow highlighted ones).

I know bottom query is not going to work.

select * from "Table" where ("Side" = 'Right') or ("Side" = 'Left')

How do I go about doing this?

Upvotes: 0

Views: 58

Answers (2)

Kaushik Nayak
Kaushik Nayak

Reputation: 31648

You may filter it using a WHERE & HAVING clause in a GROUP BY

SELECT *
  FROM t
    WHERE pid IN (
       SELECT pid
           FROM t
          WHERE side IN (
           'Right',
           'Left' )
     GROUP BY pid
     HAVING COUNT(DISTINCT side) = 2 )

Demo

Upvotes: 0

Mike
Mike

Reputation: 164

You need a join. If your table name is processes

select 
 p.*
from processes p

inner join processes p2
on p.pid = p2.pid 
and p.id <> p2.id
and p.side <> p2.side 

Upvotes: 2

Related Questions