zl程序教程

您现在的位置是:首页 >  其它

当前栏目

sqlalchemy 中 desc 的使用

SqlAlchemy 使用 desc
2023-09-27 14:25:18 时间

是这样:

items = Item.query.order_by(Item.date.desc()).all()

而不是这样:

items = Item.query.order_by(desc(Item.date)).all()

更不是说了一大堆SQLALCHEMY的查询方式别人看半天也没找到自己需要的。

 

参考文档:

sqlalchemy 中 desc 的使用

 

实例:

 #查询run_result表所有记录,并按照id进行 降序排序
 ids=session.query(RunResult).order_by(RunResult.id.desc()).all()

结果:

[<RunResult.3>, <RunResult.2>, <RunResult.1>]

输出最大的id:

#获得run_result表最大的id
id=session.query(RunResult).order_by(RunResult.id.desc()).all()[0].id
#结果
3