匹配NOT IN子句中的所有值

时间:2016-12-15 16:49:34

标签: mysql

有没有办法确保NOT IN子句中的所有值都匹配?

示例:

我有一个表DateValue,其中包含2列日期和值

ID========  DATE   ============VALUE
1========2015-01-01=========== 12
2========2015-01-01=========== 13
3========2015-01-01=========== 15
4========2015-01-01=========== 16
5========2015-01-02========== 15
6========2015-01-04=========== 15
7========2015-01-05=========== 16
8========2015-01-06=========== 12
9========2015-01-06=========== 13
10========2015-01-06=========== 15

如何在同一天选择VALUE not = 12,13和15的所有行

=>这意味着我会回来:

5========2015-01-02========== 15
6========2015-01-04=========== 15
7========2015-01-05=========== 16

2 个答案:

答案 0 :(得分:0)

我找到了解决方案:

SELECT * 
FROM `DateValue` 
WHERE `Date` NOT IN (
    SELECT DISTINCT `Date`
    FROM `result` 
    WHERE VALUE IN (12,13,15)
)

谁有更好的解决方案(不要使用子查询)?

答案 1 :(得分:0)

在第一步中,您可以找到VALUE包含所有值(121315)的日期:

SELECT `DATE`
FROM DateValue
WHERE `VALUE` IN (12, 13, 15)
GROUP BY `DATE`
HAVING COUNT(*) = 3

您可以将其用作子查询来获取所需的值:

SELECT *
FROM DateValue
WHERE `DATE` NOT IN (
    SELECT `DATE`
    FROM DateValue
    WHERE `VALUE` IN (12, 13, 15)
    GROUP BY `DATE`
    HAVING COUNT(*) = 3
) a