4

I have a following result set:

request_id  |  p_id
66          |  10
66          |  10
66          |  10
66          |  22
66          |  22
76          |  23
76          |  24

I am trying to select rows that excludes records with certain combination values:

request_id   |   product_id
66           |   10
76           |   23

So the output result set should contain only these records:

66          |  22
66          |  22
76          |  24

I tried doing:

select * from `table` 
where request_id NOT IN (66, 76) AND product_id NOT IN (10, 22)

But this gives me empty resultset.

How do I exclude just the combination of those two values?

Azima
  • 3,835
  • 15
  • 49
  • 95

3 Answers3

8

You can try below -

DEMO

select * from `table` 
where (request_id, p_id) NOT IN ((66, 10),(76,23)) 

OUTPUT:

request_id  p_id
66          22
66          22
76          24
Fahmi
  • 37,315
  • 5
  • 22
  • 31
2

Try use something like this:

SELECT DISTINCT *
FROM TABLE1
WHERE TABLE1.request_id NOT IN
(
    SELECT r_id 
    FROM TABLE2
)
AND TABLE1.p_id NOT IN
(
    SELECT p_id 
    FROM TABLE2
)
0

That is a better way:

SELECT DISTINCT *
FROM TABLE1
LEFT JOIN TABLE2 ON TABLE1.request_id = TABLE2.r_id
                AND TABLE1.p_id = TABLE2.p_id
WHERE TABLE2.p_id IS NULL