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

后端 未结 2 1709
既然无缘
既然无缘 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    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
    

提交回复
热议问题