SQLAlchemy: show only latest result if a join returns multiple results

自作多情 提交于 2019-11-29 18:09:05
Ilja Everilä

You can use the DISTINCT ON ... ORDER BY "idiom" in Postgresql to get the , where n equals 1, given that your table isn't huge:

players_with_score = db.session.query(Player, Score).\
    join(Score).\
    distinct(Player.id).\
    order_by(Player.id, Score.timestamp.desc())

Since Score.timestamp is not nullable, you do not need to worry about descending order and NULLs.

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