select 行不在多列中 mysql

select rows where not in multiple columns mysql

我有以下结果集:

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

我正在尝试 select excludes 记录的具有特定组合值的行:

request_id   |   product_id
66           |   10
76           |   23

所以输出结果集应该只包含这些记录:

66          |  22
66          |  22
76          |  24

我试过:

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

但这给了我空的结果集。

如何排除这两个值的组合?

你可以试试下面的-

DEMO

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

输出:

request_id  p_id
66          22
66          22
76          24

尝试使用这样的东西:

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
)

这是更好的方法:

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