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(Integer)
# more not impoortant columns
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(Integer)
# more not impoortant columns
对于需要查找分数字段的最小值和最大值的情况。您可以使用 min 和 max 函数通过一个查询来执行此操作:
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
from sqlalchemy import func
max = session.query(func.max(Table.column)).scalar()
min = session.query(func.min(Table.column)).scalar()