Retrieve single identifier entries only from others

I have a table like this ...

oid id 35 1 43 1 46 1 43 2 49 2 

I have id = 1 , now I want all the records that belong to only 1 to not be different identifiers.

ie o / p - 35.46

I do not need oid = 43 bcz, it also belongs to 2.

I don’t know how to write my question in a table on stackoverflow, so please ignore my wrong query method.

thanks

+4
source share
4 answers

Try the following:

 SELECT * FROM `table` WHERE id = 1 AND oid NOT IN (SELECT oid FROM `table` where id != 1) 
+5
source

here is another way

 SELECT oid FROM tableName GROUP BY oid HAVING COUNT(DISTINCT id) = 1 AND -- counts the number of ID for OID MAX(ID) = 1 -- checks if the value of ID is equal to 1 

OUTPUT

 ╔═════╗ β•‘ OID β•‘ ╠═════╣ β•‘ 35 β•‘ β•‘ 46 β•‘ β•šβ•β•β•β•β•β• 
+3
source
 select count(oid), oid from table where id = 1 group by oid having count(oid) = 1 
+2
source
 SELECT * FROM TABLE_NAME WHERE id = 1 AND oid NOT IN (SELECT DISTINCT oid FROM TABLE_NAME where id != 1) 
+1
source

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


All Articles