Py-postgresql:WHERE param = None 不工作
Py-postgresql: WHERE param = None not working
我正在使用 python3.6
和 py-postgresql==1.2.1
。
我有以下声明:
db.prepapre("SELECT * FROM seasons WHERE user_id= AND season_id= LIMIT 1)
,其中 season_id 可以为 NULL。
我希望能够通过将 None
作为 param
传递来获取带有 NULL season_id
的最新记录,但它不起作用。相反,我需要创建第二个语句:
db.prepapre("SELECT * FROM seasons WHERE user_id= AND season_id IS NULL LIMIT 1)
这一定与 season_id = NULL
不工作有关,而 season_id IS NULL
是,但是有没有办法让它工作?
来自Comparison Functions and Operators:
Do not write expression = NULL because NULL is not “equal to” NULL. (The null value represents an unknown value, and it is not known whether two unknown values are equal.)
Some applications might expect that expression = NULL returns true if expression evaluates to the null value. It is highly recommended that these applications be modified to comply with the SQL standard. However, if that cannot be done the transform_null_equals configuration variable is available. If it is enabled, PostgreSQL will convert x = NULL clauses to x IS NULL.
和:
19.13.2. Platform and Client Compatibility
transform_null_equals (boolean)
When on, expressions of the form expr = NULL (or NULL = expr) are treated as expr IS NULL, that is, they return true if expr evaluates to the null value, and false otherwise. The correct SQL-spec-compliant behavior of expr = NULL is to always return null (unknown). Therefore this parameter defaults to off.
您可以重写您的查询:
SELECT *
FROM seasons
WHERE user_id =
AND (season_id = OR ( IS NULL AND season_id IS NULL))
-- ORDER BY ... --LIMIT without sorting could be dangerous
-- you should explicitly specify sorting
LIMIT 1;
我正在使用 python3.6
和 py-postgresql==1.2.1
。
我有以下声明:
db.prepapre("SELECT * FROM seasons WHERE user_id= AND season_id= LIMIT 1)
,其中 season_id 可以为 NULL。
我希望能够通过将 None
作为 param
传递来获取带有 NULL season_id
的最新记录,但它不起作用。相反,我需要创建第二个语句:
db.prepapre("SELECT * FROM seasons WHERE user_id= AND season_id IS NULL LIMIT 1)
这一定与 season_id = NULL
不工作有关,而 season_id IS NULL
是,但是有没有办法让它工作?
来自Comparison Functions and Operators:
Do not write expression = NULL because NULL is not “equal to” NULL. (The null value represents an unknown value, and it is not known whether two unknown values are equal.)
Some applications might expect that expression = NULL returns true if expression evaluates to the null value. It is highly recommended that these applications be modified to comply with the SQL standard. However, if that cannot be done the transform_null_equals configuration variable is available. If it is enabled, PostgreSQL will convert x = NULL clauses to x IS NULL.
和:
19.13.2. Platform and Client Compatibility
transform_null_equals (boolean)
When on, expressions of the form expr = NULL (or NULL = expr) are treated as expr IS NULL, that is, they return true if expr evaluates to the null value, and false otherwise. The correct SQL-spec-compliant behavior of expr = NULL is to always return null (unknown). Therefore this parameter defaults to off.
您可以重写您的查询:
SELECT *
FROM seasons
WHERE user_id =
AND (season_id = OR ( IS NULL AND season_id IS NULL))
-- ORDER BY ... --LIMIT without sorting could be dangerous
-- you should explicitly specify sorting
LIMIT 1;