SQLAlchemy 按降序排序?

发布于 2025-01-10 03:20:48 字数 502 浏览 0 评论 0原文

如何在 SQLAlchemy 查询中使用 ORDER BY descending ,如下所示?

此查询有效,但按升序返回它们:

query = (model.Session.query(model.Entry)
        .join(model.ClassificationItem)
        .join(model.EnumerationValue)
        .filter_by(id=c.row.id)
        .order_by(model.Entry.amount) # This row :)
        )

如果我尝试:

.order_by(desc(model.Entry.amount))

然后我得到:NameError:全局名称'desc'未定义

How can I use ORDER BY descending in a SQLAlchemy query like the following?

This query works, but returns them in ascending order:

query = (model.Session.query(model.Entry)
        .join(model.ClassificationItem)
        .join(model.EnumerationValue)
        .filter_by(id=c.row.id)
        .order_by(model.Entry.amount) # This row :)
        )

If I try:

.order_by(desc(model.Entry.amount))

then I get: NameError: global name 'desc' is not defined.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

海夕 2025-01-17 03:20:48

仅供参考,您还可以将这些内容指定为列属性。例如,我可能会这样做:

.order_by(model.Entry.amount.desc())

这很方便,因为它避免了导入,并且您可以在其他地方使用它,例如在关系定义中等。

有关更多信息,您可以参考此< a href="https://docs.sqlalchemy.org/en/14/core/sqlelement.html#sqlalchemy.sql.expression.desc" rel="noreferrer">SQLAlchemy 1.4 文档

Just as an FYI, you can also specify those things as column attributes. For instance, I might have done:

.order_by(model.Entry.amount.desc())

This is handy since it avoids an import, and you can use it on other places such as in a relation definition, etc.

For more information, you can refer this SQLAlchemy 1.4 Documentation

苍风燃霜 2025-01-17 03:20:48
from sqlalchemy import desc
someselect.order_by(desc(table1.mycol))

用法来自@jpmc26

from sqlalchemy import desc
someselect.order_by(desc(table1.mycol))

Usage from @jpmc26

岁月打碎记忆 2025-01-17 03:20:48

您可以做的另一件事是:

from sqlalchemy.sql import text
...
.order_by(text("name desc")

这将导致:ORDER BY name desc。这里的缺点是 order by 使用的显式列名。

One other thing you might do is:

from sqlalchemy.sql import text
...
.order_by(text("name desc")

This will result in: ORDER BY name desc. The disadvantage here is the explicit column name used in order by.

桜花祭 2025-01-17 03:20:48

您可以在查询中使用 .desc() 函数,如下所示:

query = (model.Session.query(model.Entry)
        .join(model.ClassificationItem)
        .join(model.EnumerationValue)
        .filter_by(id=c.row.id)
        .order_by(model.Entry.amount.desc())
        )

这将按金额降序排列

query = session.query(
    model.Entry
).join(
    model.ClassificationItem
).join(
    model.EnumerationValue
).filter_by(
    id=c.row.id
).order_by(
    model.Entry.amount.desc()
)
)

使用 SQLAlchemy 的 desc 函数:

from sqlalchemy import desc
query = session.query(
    model.Entry
).join(
    model.ClassificationItem
).join(
    model.EnumerationValue
).filter_by(
    id=c.row.id
).order_by(
    desc(model.Entry.amount)
)
)

对于官方文档,请使用 链接 或检查以下代码片段:

sqlalchemy.sql.expression.desc(column) 产生降序 ORDER BY
子句元素。

例如:

from sqlalchemy import desc

stmt = select([users_table]).order_by(desc(users_table.c.name))

将生成 SQL 为:

从用户中选择 ID、姓名 ORDER BY 姓名 DESC

desc() 函数是
ColumnElement.desc() 方法可用于所有 SQL 表达式,例如:

stmt = select([users_table]).order_by(users_table.c.name.desc())

参数列 – ColumnElement(例如标量 SQL 表达式)
应用 desc() 操作。

另请参阅

asc()

nullsfirst()

nullslast()

Select.order_by()

You can use .desc() function in your query just like this:

query = (model.Session.query(model.Entry)
        .join(model.ClassificationItem)
        .join(model.EnumerationValue)
        .filter_by(id=c.row.id)
        .order_by(model.Entry.amount.desc())
        )

This will order by amount in descending order
or

query = session.query(
    model.Entry
).join(
    model.ClassificationItem
).join(
    model.EnumerationValue
).filter_by(
    id=c.row.id
).order_by(
    model.Entry.amount.desc()
)
)

Use of desc function of SQLAlchemy:

from sqlalchemy import desc
query = session.query(
    model.Entry
).join(
    model.ClassificationItem
).join(
    model.EnumerationValue
).filter_by(
    id=c.row.id
).order_by(
    desc(model.Entry.amount)
)
)

For official docs please use the link or check below snippet:

sqlalchemy.sql.expression.desc(column) Produce a descending ORDER BY
clause element.

e.g.:

from sqlalchemy import desc

stmt = select([users_table]).order_by(desc(users_table.c.name))

will produce SQL as:

SELECT id, name FROM user ORDER BY name DESC

The desc() function is a standalone version of the
ColumnElement.desc() method available on all SQL expressions, e.g.:

stmt = select([users_table]).order_by(users_table.c.name.desc())

Parameters column – A ColumnElement (e.g. scalar SQL expression) with
which to apply the desc() operation.

See also

asc()

nullsfirst()

nullslast()

Select.order_by()

如日中天 2025-01-17 03:20:48

您可以尝试:.order_by(ClientTotal.id.desc())

session = Session()
auth_client_name = 'client3' 
result_by_auth_client = session.query(ClientTotal).filter(ClientTotal.client ==
auth_client_name).order_by(ClientTotal.id.desc()).all()

for rbac in result_by_auth_client:
    print(rbac.id) 
session.close()

You can try: .order_by(ClientTotal.id.desc())

session = Session()
auth_client_name = 'client3' 
result_by_auth_client = session.query(ClientTotal).filter(ClientTotal.client ==
auth_client_name).order_by(ClientTotal.id.desc()).all()

for rbac in result_by_auth_client:
    print(rbac.id) 
session.close()
枫以 2025-01-17 03:20:48

补充@Radu的回答,就像在SQL中一样,如果你有很多具有相同属性的表,你可以在参数中添加表名。

.order_by("TableName.name desc")

Complementary at @Radu answer, As in SQL, you can add the table name in the parameter if you have many table with the same attribute.

.order_by("TableName.name desc")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文