ori tabib
ori tabib

Reputation: 13

select values with same id but different in other column

I've got this table.

OWNS:

Pid Cid
100 1
100 2
200 1
200 3
300 4
400 1
400 4
400 5
500 2
500 4

I need to select only Pid who have Cid = 1 but not Cid = 2 the output should be:

pid
200
400

My query is:

SELECT Pid
FROM OWNS 
WHERE Cid = 1 and Cid <> 2

but I get result :

pid
100
200
400

Upvotes: 1

Views: 320

Answers (1)

Mihai Ovidiu Drăgoi
Mihai Ovidiu Drăgoi

Reputation: 1317

Try

SELECT distinct Pid 
FROM OWNS 
WHERE 
   Pid not in (select Pid from OWNS where Cid = 2) 
AND CID = 1;

Upvotes: 1

Related Questions