比如说,我想按名字对记录进行排序,名字也可以从数字字母开始。
如果我这么做
Something.query.order_by(Something.name)我有以下命令:
如何将查询更改为将数字放在字母之后:
version
发布于 2019-10-19 19:47:11
添加一个初始排序子句来将前导-数字与前导-非数字分隔起来似乎是有效的:
engine = sa.create_engine(connection_url)
metadata = sa.MetaData()
mytable = sa.Table('mytable', metadata, autoload=True, autoload_with=engine)
# 1. default sort
stmt = sa.sql.select([mytable.c.thing])\
.select_from(mytable)\
.order_by(mytable.c.thing)
with engine.begin() as conn:
print(conn.execute(stmt).fetchall())
# [('1st in a row',), ('99 beers',), ('Beta version',), ('Zesty',)]
# 2. non-numeric first
sort_boundary = '9' * mytable.c.thing.type.length
print(sort_boundary)
# 99999999999999999999999999999999999999999999999999
#
stmt = sa.sql.select([mytable.c.thing])\
.select_from(mytable)\
.order_by(mytable.c.thing <= sort_boundary, mytable.c.thing)
with engine.begin() as conn:
print(conn.execute(stmt).fetchall())
# [('Beta version',), ('Zesty',), ('1st in a row',), ('99 beers',)]https://stackoverflow.com/questions/58466629
复制相似问题