本文介绍了如何获得最新记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要从db获取最后一条记录.我正在使用sqlalchemy.此刻,我正在那样做:
I need to get last record from db. I'm using sqlalchemy.At the moment, i'm doing like that:
obj = ObjectRes.query.all()
return str(obj[-1].id)
但是查询太繁琐了.我怎样才能更好地获得最近的记录?
But it's too heavy query. How can i get last record better?
推荐答案
看看.如果您在右侧列中指定 sort ,则第一个将是你的最后一个.一个示例可能看起来像这样:
Take a look at Query.first()
. If you specify a sort on the right column, the first will be your last. An example could look like this:
obj = session.query(ObjectRes).order_by(ObjectRes.id.desc()).first()
这篇关于如何获得最新记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!