最佳答案
这是我的(PostgreSQL)表——
test=> create table people (name varchar primary key,
marriage_status varchar) ;
test=> insert into people values ('Ken', 'married');
test=> insert into people values ('May', 'single');
test=> insert into people values ('Joe', NULL);
我想选择所有已知已婚的 没有人,也就是说,包括那些婚姻状态为 NULL 的人。
这是 没有的工作原理
test=> select * from people where marriage_status != 'married' ;
name | marriage_status
------+-----------------
May | single
(1 row)
当然了
test=> select * from people where marriage_status != 'married'
or marriage_status is NULL ;
name | marriage_status
------+-----------------
May | single
Joe |
问题是我正在从 SQLAlchemy 访问它——
...filter(or_(people.marriage_status!='married',
people.marriage_status is None))
翻译过来就是..
SELECT people.name as name,
people.marriage_status as marriage_status
FROM people
WHERE people.marriage_status != %(status_1)s OR False
sqlalchemy.engine.base.Engine.... {'status_1': 'married'}
没有有用吗
test=> select * from people where marriage_status != 'married'
or False;
name | marriage_status
------+-----------------
May | single
(1 row)
我也不知道
test=> select * from people where marriage_status != 'married'
or NULL;
name | marriage_status
------+-----------------
May | single
(1 row)
如何通过 SQLAlchemy 选择 NULL 值?