如何将mySQL名称放在有序列表中

发布于 2025-02-01 09:53:11 字数 957 浏览 6 评论 0原文

我有以下项目列表

类别ordernum
先决条件2
null4
先决条件6
签字8
签名10

我希望订购它,以便“先决条件”在一起,而无效类别则在此之后出现,因此:

类别ordernum
preremisites2
先决条件6
null4
签名8
登录10

当前我的SQL的订单由以下订单:

订单订购由ordernum<> ''desc,ordernum

我已经尝试了以下操作,但是最终将null放在了。

cocece(类别,'')<>> ''desc,ordernum<> ''desc,ordernum

我正在尝试实现它,以便具有相同类别的记录在记录集中在一起,null项目应出现在“签名”类别之前任何“签名”记录。

我不确定一个查询是否可以。任何帮助将不胜感激。

谢谢!

I have the following list of items

CategoryOrderNum
Prerequisites2
NULL4
Prerequisites6
Sign Off8
Sign Off10

I would like it to be ordered so that 'Prerequisites' is together and the NULL category appears after it, so that:

CategoryOrderNum
Prerequisites2
Prerequisites6
NULL4
Sign Off8
Sign Off10

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猫烠⑼条掵仅有一顆心 2025-02-08 09:53:12

您可以使用case表达式应用所有想要的条件:

SELECT * FROM tablename
ORDER BY CASE 
           WHEN Category = 'Prerequisites' THEN 1
           WHEN Category IS NULL THEN 2
           ELSE 3
         END, 
         Category,
         OrderNum;

或者,如果在类别中也有空字符串您想要用null s:

SELECT * FROM tablename
ORDER BY CASE 
           WHEN Category = 'Prerequisites' THEN 1
           WHEN COALESCE(Category, '') = '' THEN 2
           ELSE 3
         END, 
         Category,
         OrderNum;

或:

SELECT * FROM tablename
ORDER BY Category = 'Prerequisites' DESC, 
         Category IS NULL DESC, -- or: COALESCE(Category, '') = '' DESC,
         Category,
         OrderNum;

类别的最小进行对行进行排序

SELECT t1.* FROM tablename t1
ORDER BY (SELECT MIN(t2.OrderNum) FROM tablename t2 WHERE t2.Category = t1.Category),
         t1.OrderNum;

但是,如果您想要的是按最小ordernum min()窗口函数:

SELECT * FROM tablename
ORDER BY MIN(OrderNum) OVER (PARTITION BY Category),
         OrderNum;

请参阅

You can apply all the conditions that you want with a CASE expression:

SELECT * FROM tablename
ORDER BY CASE 
           WHEN Category = 'Prerequisites' THEN 1
           WHEN Category IS NULL THEN 2
           ELSE 3
         END, 
         Category,
         OrderNum;

or, if there are also empty strings in Category which you want sorted with the NULLs:

SELECT * FROM tablename
ORDER BY CASE 
           WHEN Category = 'Prerequisites' THEN 1
           WHEN COALESCE(Category, '') = '' THEN 2
           ELSE 3
         END, 
         Category,
         OrderNum;

or:

SELECT * FROM tablename
ORDER BY Category = 'Prerequisites' DESC, 
         Category IS NULL DESC, -- or: COALESCE(Category, '') = '' DESC,
         Category,
         OrderNum;

But, if what you want is to sort the rows by the minimum OrderNum of each Category use a correlated subquery:

SELECT t1.* FROM tablename t1
ORDER BY (SELECT MIN(t2.OrderNum) FROM tablename t2 WHERE t2.Category = t1.Category),
         t1.OrderNum;

or, for MySql 8.0+ use MIN() window function:

SELECT * FROM tablename
ORDER BY MIN(OrderNum) OVER (PARTITION BY Category),
         OrderNum;

See the demo.

╰沐子 2025-02-08 09:53:12

如果要在之前立即使用null值,则可以使用prerequisitesz作为null值的后备。

ORDER BY COALESCE(Category, 'PrerequisitesZ') DESC, OrderNum;

If you want the NULL values right after Prerequisites, you can use PrerequisitesZ as the fallback for NULL values:

ORDER BY COALESCE(Category, 'PrerequisitesZ') DESC, OrderNum;
茶色山野 2025-02-08 09:53:12

您可以尝试在子句中使用顺序中的两个条件:

  1. 如果类别具有“先决条件”的值,则应首先
  2. 通过order> ordernum 值

这是您将做到的:

SELECT * 
FROM tab
ORDER BY IF(Category='Prerequisites', 0, 1), 
         OrderNum

demo 在这里

Note :实际上,您可以通过子句中的条件在顺序中播放多少,因为它可以接受大多数MySQL构造。

You can try using two conditions in the ORDER BY clause:

  1. if Category has the value "Prerequisites", it should come first
  2. otherwise order by the OrderNum value

Here's how you would do it:

SELECT * 
FROM tab
ORDER BY IF(Category='Prerequisites', 0, 1), 
         OrderNum

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文