我该如何做多个"order_by"?在Flask-SQLAlchemy中? [英] How can I do multiple "order_by" in Flask-SQLAlchemy?

查看:1086
本文介绍了我该如何做多个"order_by"?在Flask-SQLAlchemy中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个User模型,其中的字段为popularitydate_created.我要执行以下查询:

Let's say I have a User model with fields popularity and date_created. I want to do the following query:

SELECT * FROM user ORDER BY popularity DESC, date_created DESC LIMIT 10

在SQLAlchemy中,仅此一项有效:

In SQLAlchemy, for a single one this works:

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

我应该再添加一个order_by()吗?还是将popularitydate_created都放在我当前的order_by()中?

Should I just add another order_by()? Or put both popularity and date_created in my current order_by()?

我希望popularitydate_created上具有优先订购权.

I want popularity to have priority on date_created for ordering.

推荐答案

这应该有效

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

这篇关于我该如何做多个"order_by"?在Flask-SQLAlchemy中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆