How to select min and max from table by column score?

后端 未结 2 1719
既然无缘
既然无缘 2021-02-13 05:23

How to select min and max from table by column score ? Is this possible with session query ?

class Player(Base):
    username = Column(String)
    score = Column         


        
相关标签:
2条回答
  • 2021-02-13 05:45

    For the case when you need to find minimum and maximum values for the score field. You can do this with a one query using min and max functions:

    from sqlalchemy.sql import func
    qry = session.query(func.max(Player.score).label("max_score"), 
                    func.min(Player.score).label("min_score"),
                    )
    res = qry.one()
    max = res.max_score
    min = res.min_score
    
    0 讨论(0)
  • 2021-02-13 05:54
    from sqlalchemy import func
    max = session.query(func.max(Table.column)).scalar()
    min = session.query(func.min(Table.column)).scalar()
    
    0 讨论(0)
提交回复
热议问题