Home > OS >  Selecting distinct unaffected value with a condition
Selecting distinct unaffected value with a condition

Time:10-13

Hi I am new to using SQL.

I have data table like this:

ID | code
----------
1  |  1
1  |  2
1  |  4
2  |  3
2  |  3
2  |  4

I need to find DISTINCT ID that satisfies a condition of: code associated with the ID does not equal 1 or 2.

The result should tell me that ID of 2 satisfies this condition, as ID of 1 has value of 1 or 2 in the code column

CodePudding user response:

If I understand your question correct(find records from ID and ID not have any value of 1 or 2),you then use IN to do it

SELECT * FROM yourtable WHERE ID NOT IN
(
  SELECT ID FROM yourtable WHERE code IN(1,2)
)
  • Related