如何在sqlalchemy中执行LIKE查询?

时间:2014-07-18 10:04:28

标签: python mysql sql select sqlalchemy

我正在使用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处出错。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分: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