前端之家收集整理的这篇文章主要介绍了
python – SQLAlchemy过滤器查询由相关对象,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_
301_0@
使用
SQLAlchemy,我与两个表 –
用户和分数有一对多的关系.我试图根据他们的总分在过去X天的天数中
查询排名前10位的
用户.
- users:
- id
- user_name
- score
-
- scores:
- user
- score_amount
- created
我当前的查询是:
- top_users = DBSession.query(User).options(eagerload('scores')).filter_by(User.scores.created > somedate).order_by(func.sum(User.scores).desc()).all()
我知道这显然不正确,这只是我最好的猜测.然而,在查看文档和谷歌搜索后,我找不到答案.
编辑:
如果我勾画出MySQL查询的样子,这可能会有所帮助
- SELECT user.*,SUM(scores.amount) as score_increase
- FROM user LEFT JOIN scores ON scores.user_id = user.user_id
- WITH scores.created_at > someday
- ORDER BY score_increase DESC