Python SQLAlchemy 按降序排序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4186062/
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
SQLAlchemy ORDER BY DESCENDING?
提问by AP257
How can I use ORDER BY descendingin a SQLAlchemy query like the following?
如何descending在 SQLAlchemy 查询中使用 ORDER BY ,如下所示?
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.
然后我得到:NameError: global name 'desc' is not defined。
采纳答案by AP257
回答by Rick
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.
这很方便,因为它避免了import,并且您可以在其他地方使用它,例如在关系定义等中。
For more information, you can refer this
有关更多信息,您可以参考此
回答by Radu
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.
这将导致: ORDER BY name desc。这里的缺点是按顺序使用的显式列名。
回答by anand tripathi
You can use .desc()function in your query just like this
您可以.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())
)
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
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)
)
)
For official docs please use the linkor 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 DESCThe 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()
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 DESCdesc() 函数是 ColumnElement.desc() 方法的独立版本,可用于所有 SQL 表达式,例如:
stmt = select([users_table]).order_by(users_table.c.name.desc())参数 column – 用于应用 desc() 操作的 ColumnElement(例如标量 SQL 表达式)。
也可以看看
升序()
空优先()
nullslast()
Select.order_by()
回答by Michael
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.
补充@Radu 答案,与在 SQL 中一样,如果您有多个具有相同属性的表,则可以在参数中添加表名。
.order_by("TableName.name desc")
回答by Artem Baranov
You can try: .order_by(ClientTotal.id.desc())
你可以试试: .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()

