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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 00:07:42  来源:igfitidea点击:

How can I do multiple "order_by" in Flask-SQLAlchemy?

postgresqlsqlalchemyflaskflask-sqlalchemy

提问by Noé Malzieu

Let's say I have a Usermodel with fields popularityand 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 popularityand date_createdin my current order_by()?

我应该添加另一个order_by()吗?或者把popularity和都date_created放在我的电流中order_by()

I want popularityto have priority on date_createdfor ordering.

我想popularity优先date_created订购。

回答by codegeek

This should work

这应该工作

User.query.order_by(User.popularity.desc(),User.date_created.desc()).limit(10).all()