SQL:按优先级排序,但最后放置0 [英] SQL: Sort by priority, but put 0 last

查看:290
本文介绍了SQL:按优先级排序,但最后放置0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个(int)列,称为优先级".选择我的项目时,我希望最高优先级(最低编号)排在最前,而最低优先级(最高编号)在倒数第二.

I have a (int) column called "priority". When I select my items I want the highest priority (lowest number) to be first, and the lowest priority (highest number) to be the last.

但是,没有优先级(当前优先级为0)的项目应在具有优先级的项目之后的其他列中列出.

However, the items without a priority (currently priority 0) should be listed by some other column after the ones with a priority.

换句话说.如果我有这些优先事项:

In other words. If I have these priorities:

 1 2 0 0 5 0 8 9

我如何像这样对它们进行排序:

How do I sort them like this:

 1 2 5 8 9 0 0 0 

我想我可以使用Int.max而不是0,但是0构成了一个很好的默认值,我会尝试保留.

I guess I could use Int.max instead of 0, but 0 makes up such a nice default value which I would try to keep.

推荐答案

尝试:

order by case priority when 0 then 2 else 1 end, priority

这篇关于SQL:按优先级排序,但最后放置0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆