有3行
| Name | State |
| 1 | Busy |
| 2 | Online |
| 3 | Offline |
我想优先考虑"在线" > "忙" > "离线"
heroes = HeroModel.query.\
filter(HeroModel.id.in_(hero_ids)).\
order_by(???????). \
all()
我该如何订购?不按字母顺序排列。
答案 0 :(得分:0)
在您的选择查询中使用类似的内容
case state
when 'online' then 1
when 'offline' then 2
else 3
end as rnk
并在此派生列rnk
order by rnk
答案 1 :(得分:0)
您可以在SQLAlchemy中尝试此操作(如果您使用Oracle或MySQL):
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()