布尔运算符行为

Booleans operators behavior

编辑:选择后我的查询出现错误。离开这里是因为无论如何答案都很有趣。


我想为 MS SQL 服务器创建一个过滤器,例如:

WHERE ((this IS NOT NULL) or (that IS NOT NULL))

我试过回复 given at a similar question:

filter((TAB1.c.this.isnot(None)) | (TAB2.c.that.isnot(None)))

filter(or_(TAB1.c.PERSONNE_ID.isnot(None), (TAB2.c.PERSONNE_ID.isnot(None)))

但使用这些选项我只能得到:

WHERE (this IS NOT NULL or that IS NOT NULL)

在 T-SQL 中,这是一个与我想要获得的过滤器不同的过滤器。

有什么提示吗?

这两个 WHERE 子句是相同的,因为 IS NOT NULL 的运算符优先级高于 OR,因此在 OR 被求值之前先被求值。

http://sqlfiddle.com/#!18/c0e9d/7

CREATE TABLE test (
  id INT,
  this INT,
  that INT
);
INSERT INTO test (id, this, that) VALUES (1, 2, NULL);
INSERT INTO test (id, this, that) VALUES (2, NULL, NULL);
INSERT INTO test (id, this, that) VALUES (3, NULL, 4);
INSERT INTO test (id, this, that) VALUES (4, 5, 6);

导致

SELECT * FROM test WHERE ((this IS NOT NULL) or (that IS NOT NULL));
SELECT * FROM test WHERE (this IS NOT NULL) or (that IS NOT NULL);
SELECT * FROM test WHERE (this IS NOT NULL or that IS NOT NULL);
SELECT * FROM test WHERE this IS NOT NULL or that IS NOT NULL;

全部返回第 1、3 和 4 行,仅省略具有两个值的项目 2 NULL