from sqlalchemy import text
class Test(db.Model):
__tablename__ = 'test'
id = db.Column(db.Integer, primary_key=True)
col_1 = db.Column(db.Integer)
col_2 = db.Column(db.Integer)
with app.app_context():
# 倒序
test = Test.query.with_entities(Test.id, Test.col_1, Test.col_2).order_by(Test.id.desc()).all()
# 多字段排序
test = Test.query.with_entities(Test.id, Test.col_1, Test.col_2).order_by(Test.id).order_by(Test.col_1).all()
# 或者
test = Test.query.with_entities(Test.id, Test.col_1, Test.col_2).order_by(Test.id, Test.col_1).all()
# 使用text
with app.app_context():
## 按查询的第一个排序
test = Test.query.with_entities(Test.id, Test.col_1, Test.col_2).order_by(text('1')).all()
## 按查询的第一个倒序排序
test = Test.query.with_entities(Test.id, Test.col_1, Test.col_2).order_by(text('1 desc')).all()
## 多字段排序
test = Test.query.with_entities(Test.col_1, Test.col_2).order_by(text('1 desc'), text('2')).all()
## 或者
test = Test.query.with_entities(Test.col_1, Test.col_2).order_by(text('1 desc, 2 desc')).all()
## 按col_1排序
test = Test.query.with_entities(Test.id, Test.col_1, Test.col_2).order_by(text('col_1')).all()
## 按col_1倒序排序
test = Test.query.with_entities(Test.id, Test.col_1, Test.col_2).order_by(text('-col_1')).all()
07-14
8590
08-17
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交