【问题标题】:SQLAlchemy Multiple Where Condition [duplicate]SQLAlchemy多个Where条件[重复]
【发布时间】:2022-02-12 03:12:42
【问题描述】:
我有一个订单表。我想用多个条件向这个表写一个查询。例如,我想得到一个带有id字段1和order_status 字段3的订单。
有没有办法用 SQL Alchemy 做到这一点?
附:我只能通过以下查询检查一个条件。
order = Orders.query.filter_by(restaurant_id = restaurant_id).all()
【问题讨论】:
标签:
python
flask-sqlalchemy
【解决方案1】:
有多种方法可以做到这一点,例如,如果你想应用一个和操作,你可以简单地使用下面的过滤器链
order = Orders.query.filter(restaurant_id = restaurant_id).filter(id = 1).filter(order_status = "Status").all()
您还可以使用and_ 和or_ 来应用如下相应的条件。
cond = and_(restaurant_id = restaurant_id, id = 1)
order = Orders.query.filter(or_(cond, order_status = "Status")).all()