如何将mySQL名称放在有序列表中
我有以下项目列表
类别 | ordernum |
---|---|
先决条件 | 2 |
null | 4 |
先决条件 | 6 |
签字 | 8 |
签名 | 10 |
我希望订购它,以便“先决条件”在一起,而无效类别则在此之后出现,因此:
类别 | ordernum |
---|---|
preremisites | 2 |
先决条件 | 6 |
null | 4 |
签名 | 8 |
登录 | 10 |
当前我的SQL的订单由以下订单:
订单订购由ordernum<> ''desc,ordernum
我已经尝试了以下操作,但是最终将null放在了。
cocece(类别,'')<>> ''desc,ordernum<> ''desc,ordernum
我正在尝试实现它,以便具有相同类别的记录在记录集中在一起,null项目应出现在“签名”类别之前任何“签名”记录。
我不确定一个查询是否可以。任何帮助将不胜感激。
谢谢!
I have the following list of items
Category | OrderNum |
---|---|
Prerequisites | 2 |
NULL | 4 |
Prerequisites | 6 |
Sign Off | 8 |
Sign Off | 10 |
I would like it to be ordered so that 'Prerequisites' is together and the NULL category appears after it, so that:
Category | OrderNum |
---|---|
Prerequisites | 2 |
Prerequisites | 6 |
NULL | 4 |
Sign Off | 8 |
Sign Off | 10 |
Currently my SQL has the following order by:
ORDER BY OrderNum <> '' DESC, OrderNum
I've tried the following, however it puts NULL at the end.
ORDER BY COALESCE(Category,'') <> '' DESC, OrderNum <> '' DESC, OrderNum
I'm trying to achieve it so that the records with the same category are together in the recordset, the NULL item should appear before the 'Sign Off' category because the OrderNum of NULL is less than any of the 'Sign Off' records.
I'm not sure if that's possible in one query. Any help would be appreciated.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
case
表达式应用所有想要的条件:或者,如果在
类别中也有空字符串
您想要用null s:
或:
类别的最小进行对行进行排序
但是,如果您想要的是按最小
ordernum
min()
窗口函数:请参阅。
You can apply all the conditions that you want with a
CASE
expression:or, if there are also empty strings in
Category
which you want sorted with theNULL
s:or:
But, if what you want is to sort the rows by the minimum
OrderNum
of eachCategory
use a correlated subquery:or, for MySql 8.0+ use
MIN()
window function:See the demo.
如果要在
之前立即使用
null
值,则可以使用prerequisitesz
作为null
值的后备。If you want the
NULL
values right afterPrerequisites
, you can usePrerequisitesZ
as the fallback forNULL
values:您可以尝试在子句中使用
顺序中的两个条件:
类别
具有“先决条件”的值,则应首先order> ordernum 值
这是您将做到的:
demo 在这里。
Note :实际上,您可以通过子句中的条件在
顺序中播放多少,因为它可以接受大多数MySQL构造。
You can try using two conditions in the
ORDER BY
clause:Category
has the value "Prerequisites", it should come firstOrderNum
valueHere's how you would do it:
Demo here.
Note: Actually you can play how much you want with the conditions in the
ORDER BY
clause, as it can accept most of MySQL constructs.