简体   繁体   English

选择不在多列mysql中的行

[英]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: 我试图选择excludes具有特定组合值的记录的行:

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? 如何只排除这两个值的组合?

You can try below - 你可以尝试下面 -

DEMO DEMO

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

OUTPUT: OUTPUT:

request_id  p_id
66          22
66          22
76          24

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
)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM