【发布时间】:2017-09-20 02:44:48
【问题描述】:
我有下一个查询。
item = [item.export_simple()
for item in session.query(Item)
.filter(and_(
Item.companyId == company_id,
or_(
True if search == "" else None,
or_(*[Item.name.like('%{0}%'.format(s)) for s in words]),
or_(*[Item.code.like('%{0}%'.format(s)) for s in words])
))).order_by(Item.name)]
还有这个。
if type == "code":
src = [Item.code.like('%{0}%'.format(s)) for s in words]
elif type == "name":
src = [Item.name.like('%{0}%'.format(s)) for s in words]
session.query(Item)
.filter(and_(
Item.companyId == company_id,
Item.typeItem == item_type,
or_(
True if search == "" else None,
or_(*src)
)))
在这两种情况下,我在 or_() 语句中都有* 运算符,并且两个查询都工作得很好,但我不知道具体原因。
这是reference 和this one
【问题讨论】:
标签: python sqlalchemy