sqlalchemy 不是 NULL 选择 [英] sqlalchemy IS NOT NULL select

查看:18
本文介绍了sqlalchemy 不是 NULL 选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何像在 SQL 中一样添加过滤器以从特定列中选择非 NULL 的值?

SELECT *发件人表你的列不为空的地方;

如何使用 SQLAlchemy 过滤器做同样的事情?

select = select(table).select_from(table).where(all_filters)

解决方案

column_obj != None 将产生一个 IS NOT NULL 约束:

<块引用>

在列上下文中,生成子句 a != b.如果目标是 None,则生成 IS NOT NULL.

或使用 isnot()(0.7.9 新增):

<块引用>

实现 IS NOT 运算符.

通常,当与解析为 NULLNone 值进行比较时,会自动生成 IS NOT.但是,如果与某些平台上的布尔值进行比较,可能需要显式使用 IS NOT.

演示:

<预><代码>>>>从 sqlalchemy.sql 导入列>>>column('YourColumn') != None<sqlalchemy.sql.elements.BinaryExpression 对象在 0x10c8d8b90>>>>str(column('YourColumn') != None)'"YourColumn" 不是 NULL'>>>column('YourColumn').isnot(None)<sqlalchemy.sql.elements.BinaryExpression 对象在 0x104603850>>>>str(column('YourColumn').isnot(None))'"YourColumn" 不是 NULL'

How can I add the filter as in SQL to select values that are NOT NULL from a certain column ?

SELECT * 
FROM table 
WHERE YourColumn IS NOT NULL;

How can I do the same with SQLAlchemy filters?

select = select(table).select_from(table).where(all_filters) 

解决方案

column_obj != None will produce a IS NOT NULL constraint:

In a column context, produces the clause a != b. If the target is None, produces a IS NOT NULL.

or use isnot() (new in 0.7.9):

Implement the IS NOT operator.

Normally, IS NOT is generated automatically when comparing to a value of None, which resolves to NULL. However, explicit usage of IS NOT may be desirable if comparing to boolean values on certain platforms.

Demo:

>>> from sqlalchemy.sql import column
>>> column('YourColumn') != None
<sqlalchemy.sql.elements.BinaryExpression object at 0x10c8d8b90>
>>> str(column('YourColumn') != None)
'"YourColumn" IS NOT NULL'
>>> column('YourColumn').isnot(None)
<sqlalchemy.sql.elements.BinaryExpression object at 0x104603850>
>>> str(column('YourColumn').isnot(None))
'"YourColumn" IS NOT NULL'

这篇关于sqlalchemy 不是 NULL 选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆