sqlalchemy: get max/min/avg values from a table

前端 未结 2 1295
栀梦
栀梦 2020-12-16 11:07

I have this query:

mps =   (
            session.query(mps)  .filter_by(idc = int(c.idc))
                                .filter_by(idmp = int(m.idmp))
             


        
相关标签:
2条回答
  • 2020-12-16 11:47

    The following functions are available with from sqlalchemy import func:

    • func.min
    • func.max
    • func.avg

    Documentation is available here.

    You can use them i.e. in the query() method.

    Example:

    session.query(self.stats.c.ID, func.max(self.stats.c.STA_DATE))
    

    (just like you use agragate functions in plain SQL)

    0 讨论(0)
  • 2020-12-16 11:50

    Or just use an order_by() and select the first or last element. . .

    0 讨论(0)
提交回复
热议问题