以下是我正在处理的查询:
SELECT TBL_SUB_KEY AS port
, poe.[TBL_COMPANY]
, poe.[TBL_DIVISION_1]
FROM dbo.TMVKTAB AS poe
WHERE ( TBL_NUMBER = '8A' )
AND ( TBL_SUB_KEY <> '' )
AND ( poe.[TBL_COMPANY] <> '011'
AND poe.[TBL_DIVISION_1] <> '11'
)
我想要归还的是公司='011'/ Division_1'= '11'中不的所有记录。
我认为在()中组合公司/部门会实现这一点,但事实并非如此。它消除了所有公司的'011'记录,并且它消除了所有分区'11'记录。
然而,当我这样做时:
SELECT TBL_SUB_KEY AS port
, poe.[TBL_COMPANY]
, poe.[TBL_DIVISION_1]
FROM dbo.TMVKTAB AS poe
WHERE ( TBL_NUMBER = '8A' )
AND ( TBL_SUB_KEY <> '' )
AND NOT ( poe.[TBL_COMPANY] = '011'
AND poe.[TBL_DIVISION_1] = '11'
)
它似乎能够得出正确的结果。这是为什么?
答案 0 :(得分:5)
这是一个布尔逻辑问题:
NOT (A and B) <=> NOT A OR NOT B
即:
NOT ( poe.[TBL_COMPANY] = '011' AND poe.[TBL_DIVISION_1] = '11')
相当于:
( poe.[TBL_COMPANY] <> '011' OR poe.[TBL_DIVISION_1] <> '11')
读:
答案 1 :(得分:3)
我认为NOT的等价物将使用OR,如下所示:
SELECT TBL_SUB_KEY AS port
, poe.[TBL_COMPANY]
, poe.[TBL_DIVISION_1]
FROM dbo.TMVKTAB AS poe
WHERE ( TBL_NUMBER = '8A' )
AND ( TBL_SUB_KEY <> '' )
AND ( poe.[TBL_COMPANY] <> '011'
OR poe.[TBL_DIVISION_1] <> '11'
)
答案 2 :(得分:2)
where x and (y and z)
与
相同x and y and z
然而
where x and not (y and z)
是你想要的,也是有效的。
试试这个:
SELECT TBL_SUB_KEY AS port
, poe.[TBL_COMPANY]
, poe.[TBL_DIVISION_1]
FROM dbo.TMVKTAB AS poe
WHERE ( TBL_NUMBER = '8A' )
AND ( TBL_SUB_KEY <> '' )
AND NOT ( poe.[TBL_COMPANY] = '011'
AND poe.[TBL_DIVISION_1] = '11'
)
答案 3 :(得分:1)
一些布尔代数的时间。这样:
not (x and y)
与:
相同(not x or not y)
所以你的查询应该是:
SELECT TBL_SUB_KEY AS port
, poe.[TBL_COMPANY]
, poe.[TBL_DIVISION_1]
FROM dbo.TMVKTAB AS poe
WHERE ( TBL_NUMBER = '8A' )
AND ( TBL_SUB_KEY <> '' )
AND ( poe.[TBL_COMPANY] <> '011'
OR poe.[TBL_DIVISION_1] <> '11'
)
答案 4 :(得分:0)
因为不属于两者公司011和第11部分的记录与
相匹配 poe.[TBL_COMPANY] <> '011'
OR poe.[TBL_DIVISION_1] <> '11'
如果公司不是011,分区可以是11,你仍然会有匹配组合。
De Morgan's law解释了如何以这种方式转换布尔表达式。