postgresql 如何在 Flask-SQLAlchemy 中执行多个“order_by”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15791760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I do multiple "order_by" in Flask-SQLAlchemy?
提问by Noé Malzieu
Let's say I have a User
model with fields popularity
and date_created
. I want to do the following query:
假设我有一个User
带有字段popularity
和的模型date_created
。我想做以下查询:
SELECT * FROM user ORDER BY popularity DESC, date_created DESC LIMIT 10
In SQLAlchemy, for a single one this works:
在 SQLAlchemy 中,对于单个这样的工作:
User.query.order_by(User.popularity.desc()).limit(10).all()
Should I just add another order_by()
? Or put both popularity
and date_created
in my current order_by()
?
我应该添加另一个order_by()
吗?或者把popularity
和都date_created
放在我的电流中order_by()
?
I want popularity
to have priority on date_created
for ordering.
我想popularity
优先date_created
订购。
回答by codegeek
This should work
这应该工作
User.query.order_by(User.popularity.desc(),User.date_created.desc()).limit(10).all()