Python 在炼金术中按优先级排序有可能吗?
有三排Python 在炼金术中按优先级排序有可能吗?,python,sql,sqlalchemy,Python,Sql,Sqlalchemy,有三排 | Name | State | | 1 | Busy | | 2 | Online | | 3 | Offline | 我想优先选择“在线”>“忙”>“离线” 我怎么点菜?不是按字母顺序。在select查询中使用类似的内容 case state when 'online' then 1 when 'offline' then 2 else 3 end as rnk 并在此派生列rnk order by rnk 您可以在SQLAlc
| Name | State |
| 1 | Busy |
| 2 | Online |
| 3 | Offline |
我想优先选择“在线”>“忙”>“离线”
我怎么点菜?不是按字母顺序。在select查询中使用类似的内容
case state
when 'online' then 1
when 'offline' then 2
else 3
end as rnk
并在此派生列rnk
order by rnk
您可以在SQLAlchemy中尝试这一点(如果您使用Oracle或MySQL):
您连接到哪个数据库?您应该使用整数表示状态,这样排序就很容易了。
order by rnk
heroes = HeroModel.query.\
filter(HeroModel.id.in_(hero_ids)).\
order_by(
case(
[(HeroModel.state == "online", 0),
(HeroModel.state == "busy", 1),
(HeroModel.state == "offline", 2)],
else_=3). \
all()