本文介绍了SQLAlchemy IN 子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在 sqlalchemy 中执行此查询
I'm trying to do this query in sqlalchemy
SELECT id, name FROM user WHERE id IN (123, 456)
我想在执行时绑定列表[123, 456]
.
I would like to bind the list [123, 456]
at execution time.
推荐答案
怎么样
session.query(MyUserClass).filter(MyUserClass.id.in_((123,456))).all()
编辑:如果没有 ORM,它将是
edit: Without the ORM, it would be
session.execute(
select(
[MyUserTable.c.id, MyUserTable.c.name],
MyUserTable.c.id.in_((123, 456))
)
).fetchall()
select()
有两个参数,第一个是要检索的字段列表,第二个是 where
条件.您可以通过 c
(或 columns
)属性访问表对象上的所有字段.
select()
takes two parameters, the first one is a list of fields to retrieve, the second one is the where
condition. You can access all fields on a table object via the c
(or columns
) property.
这篇关于SQLAlchemy IN 子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!