如何在sqlalchemy中执行LIKE查询?
我sqlalchemy
用来从表中获取数据。现在,从称为的表中获取所有记录audit_trail_table
正在按预期方式工作。
select_stmt = select([self.audit_trail_table]).where(self.audit_trail_table.c.id == int(id))row = conn.execute(select_stmt).fetchone()
现在,我希望LIKE
对多个列执行查询,如下所示:
filter_query = #(some value which will work as a filter_query while fetching )recordsfilter_stmt = select([self.audit_trail_table]).where(self.audit_trail_table.c.first_name like '%' + filter_query + '%' or self.audit_trail_table.c.last_name like '%')
但这给LIKE带来了错误。我怎样才能做到这一点?
回答:
请像运营商一样使用
select([self.audit_trail_table]).where(self.audit_trail_table.c.first_name.like('%' + filter_query + '%') or self.audit_trail_table.c.last_name.like('%abc%'))
http://docs.sqlalchemy.org/en/rel_0_9/core/sqlelement.html#sqlalchemy.sql.operators.ColumnOperators.like
以上是 如何在sqlalchemy中执行LIKE查询? 的全部内容, 来源链接: utcz.com/qa/399377.html