[英]Select rows where a column matches an element of a list SQLAlchemy
I have two tables with the following columns: 我有两个包含以下列的表:
con
---
id
code
pa
---
con_id (foreign key and references con.id)
data
I also have a list: 我也有一个清单:
names = ['a', 'b', 'c']
I want to join the two tables to get a table/view with three columns: 我想加入这两个表来获得一个包含三列的表/视图:
pa.con_id
pa.data
con.code
and return the rows where con.code
is one of the elements in names
. 并返回其中
con.code
是names
中的元素之一的行。 At the moment, I can join my tables and return all the columns: 目前,我可以加入我的表并返回所有列:
engine = create_engine('postgresql://postgres:postgres@localhost/db')
metadata = MetaData(bind=engine)
pa = Table('pa', metadata, autoload=True)
contract = Table('contract', metadata, autoload=True)
res = pa.join(contract).select().execute()
but I don't know how to do a where
which matches one of the items in my names
list. 但我不知道该怎么做了
where
它匹配在我的项目之一names
列表。
Use the in_
operator of Column
to do column in (....)
; 使用
Column
的in_
运算符来执行column in (....)
; then use it in filter with your names
list. 然后在你的
names
列表的过滤器中使用它。
pa.join(contract).select().where(contract.c.code.in_(names)).execute()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.