編輯:此選擇后,我的查詢出錯。離開這里是因為無論如何答案都很有趣。
我想為 MS SQL Server 創建一個過濾器,例如:
WHERE ((this IS NOT NULL) or (that IS NOT NULL))
我嘗試了在類似問題中給出的答復:
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 中,這是一個與我想要獲得的過濾器不同的過濾器。
任何提示?
uj5u.com熱心網友回復:
這兩個WHERE子句是相同的,因為IS NOT NULL運算子的優先級高于運算子優先級OR,因此在評估之前首先OR評估。
見http://sqlfiddle.com/#!18/c0e9d/2
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;
全部回傳第 1、3 和 4 行,僅省略具有兩個值的第 2 項NULL。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/362905.html
標籤:Python 查询语句 sqlalchemy
下一篇:查詢每個商店的總金額
