How to use a labelled column in sqlalchemy filter?

牧云@^-^@ 提交于 2019-12-12 19:11:03

问题


I want to use the labelled column in sqlalchemy filter. for eg:

db.session.query(
    PartMaster.name,
    PartMaster.description,
    PartTracker.actual_length,
    func.sum(PartTracker.quantity).label('quantity')
).join(PartTracker).group_by(
    PartTracker.part_master_id,PartTracker.actual_length
).all()

I need the result with quantity > 0. please advice


回答1:


In SQL if you want to filter rows by result, you need to use HAVING instructions.

So in your case:

db.session.query(
    PartMaster.name,
    PartMaster.description,
    PartTracker.actual_length,
    func.sum(PartTracker.quantity).label('quantity')
).join(PartTracker).group_by(
    PartTracker.part_master_id,PartTracker.actual_length
).having(
    func.sum(PartTracker.quantity) > 0
)

Example from doc



来源:https://stackoverflow.com/questions/34825773/how-to-use-a-labelled-column-in-sqlalchemy-filter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!