I have a table like this:
Tb_Product_Options:
Product_Id Option_Id 1 5 1 7 2 3 3 9 3 6 Now I want to get all Product_Ids have not this values : '5,9'. at this example my result must be: 2 (product_id)
You can aggregation :
select Product_Id from table t group by Product_Id having sum ( Option_Id in (5,9) ) = 0; If you want all columns then you can use NOT EXISTS :
select t.* from table t where not exists (select 1 from table t1 where t1.Product_Id = t.Product_Id and t1.Option_Id in (5,9) );