问题:SQLAlchemy按降序排列?

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

此查询有效,但以升序返回:

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: global name 'desc' is not defined

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.


回答 0

与FYI一样,您也可以将这些内容指定为列属性。例如,我可能已经做过:

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

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

有关更多信息,您可以参考此

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


回答 1

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

来自@ jpmc26的用法

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

Usage from @jpmc26


回答 2

您可能要做的另一件事是:

.order_by("name desc")

这将导致:ORDER BY名称desc。这里的缺点是按顺序使用显式列名。

One other thing you might do is:

.order_by("name desc")

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


回答 3

您可以.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:

SELECT id, name FROM user ORDER BY name DESC

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

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

参数column –一个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()


回答 4

你可以试试: .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()

回答 5

@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")

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。