最新消息:20181230 VPS服务器已从Linode换到腾讯云香港,主题仍用朋友推荐的大前端D8

【已解决】SQLAlchemy中order_by默认是否是升序以及写法是否是asc

Flask crifan 2889浏览 0评论

折腾:

【已解决】Flask中SQLAlchemy去实现查询后的分页结果的排序

期间,

已经知道了:

SQLAlchemy中order_by去对query结果排序,想要降序,倒序的话,用desc()

而很多例子中:

print query2.order_by(User.name).all()
print query2.order_by(User.name.desc()).all()

感觉是:

order_by默认没有传递参数,不知道是不是就是,升序 的意思

以及,升序的话,写法是不是写成asc()

想要知道:

和desc(降序,descending)对应的,默认的,是不是叫做asc==升序的ascending

SQLAlchemy ORDER BY DESCENDING in Query object?-敲代码

Query API — SQLAlchemy 1.1 Documentation

order_by(*criterion)

apply one or more ORDER BY criterion to the query and return the newly resulting Query
All existing ORDER BY settings can be suppressed by passing None – this will suppress any ORDER BY configured on mappers as well.
Alternatively, passing False will reset ORDER BY and additionally re-allow default mapper.order_by to take place. Note mapper.order_by is deprecated

SQLAlchey order_by asc

Python sqlalchemy.asc Examples

restaurants = session.query(Restaurant).order_by(asc(Restaurant.name))

asc是函数,里面传入的是:类名和属性

postgresql – Multiple order_by sqlalchemy / Flask – Stack Overflow

User.query.order_by(User.popularity.desc(),User.date_created.desc()).limit(10).all()

SQLAlchemy ORDER BY DESCENDING? – Stack Overflow

.order_by(model.Entry.amount.desc())
另外也可以:
from sqlalchemy import desc
someselect.order_by(desc(table1.mycol))

SQLALchemy DESC ASC

终于找到官网的解释了:

Column Elements and Expressions — SQLAlchemy 1.1 Documentation

sqlalchemy.sql.expression.asc(column)

Produce an ascending ORDER BY clause element.
e.g.:
from sqlalchemy import asc
stmt = select([users_table]).order_by(asc(users_table.c.name))
will produce SQL as:
SELECT id, name FROM user ORDER BY name ASC
The asc() function is a standalone version of the ColumnElement.asc() method available on all SQL expressions, e.g.:
stmt = select([users_table]).order_by(users_table.c.name.asc())
Parameters:column – A ColumnElement (e.g. scalar SQL expression) with which to apply the asc()operation.

See also

desc()

nullsfirst()

nullslast()

Select.order_by()

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()

的确是用:

asc表示升序

-》对应的SQL的也是:

ASC升序和DESC降序

 

而对于order_by,默认不传递asc的话,

不知道是不是asc

参考:

Selectables, Tables, FROM objects — SQLAlchemy 1.1 Documentation

order_by(*clauses)

inherited from the order_by() method of GenerativeSelect
return a new selectable with the given list of ORDER BY criterion applied.
The criterion will be appended to any pre-existing ORDER BY criterion.

找了半天,也没有找到:

pre-existing的ORDER BY的criterion

在哪里。

【总结】

所以,此处暂时不去操心默认的order_by的配置了。

已经弄清楚了:

order_by可以通过传递asc()或desc()去指定使用升序或降序去排序结果。

转载请注明:在路上 » 【已解决】SQLAlchemy中order_by默认是否是升序以及写法是否是asc

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
68 queries in 0.081 seconds, using 9.45MB memory