一尘不染

如何在sqlalchemy中执行LIKE查询?

sql

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 )records
filter_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带来了错误。我怎样才能做到这一点?


阅读 593

收藏
2021-03-08

共1个答案

一尘不染

请像运营商一样使用

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

2021-03-08