Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select rows where not in multiple columns mysql

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?

like image 702
Azima Avatar asked Oct 15 '25 05:10

Azima


2 Answers

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
like image 133
Fahmi Avatar answered Oct 17 '25 21:10

Fahmi


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
)

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!