SQL 服务器 - 获取与 where 子句相反的内容

SQL Server - Getting the opposite of a where clause

我有一个数据集,我想在其中过滤掉满足特定条件的行,但事实证明它比我想象的要复杂得多。

基本上我想按照 WHERE (column1 NOT LIKE 'Z%' and column2 != '2') 的思路做一些事情,因为查询只会 return 行,其中特定行 不会 [=32] =] 在 column1 中具有类似于 'Z%' 的值,而 在 column2 中没有 的值“2”。

例如:

完整数据集

column1 | column2
-----------------
ACAL    | 2
-----------------
ZVBBU3  | 2
-----------------
FSKE2   | X

我想要的数据集

column1 | column2
-----------------
ACAL    | 2
-----------------
FSKE2   | X

然而,它并没有这样做。所以我认为可能有一种像 WHERE (!(column1 LIKE 'Z% and column2 = '2')) 这样的编程方法,其中 ! 将 return 与指定的标准相反,但这会引发语法错误。有什么办法可以达到我想要的效果吗?

你可能想要这个:

WHERE NOT (column1 LIKE 'Z%' AND column2 = '2') 

你在否定的轨道上是正确的,但应该使用 NOT 来否定而不是 !

请注意

WHERE (COLUMN1 NOT LIKE 'Z%') OR (COLUMN2 != '2') 
对于

的逻辑状态,

应该和 De Morgan's laws 一样工作

The negation of a conjunction is the disjunction of the negations.
The negation of a disjunction is the conjunction of the negations.

这可能是您要写的内容:

WHERE NOT(column1 LIKE 'Z%' AND column2 = '2')

在 SQL 服务器中使用 T-SQL,虽然它可以工作,但您应该使用:

  • 不是 => !
  • <> => !=
  • = => ==