以类似于 Excel 的方式对 MySQL 中的 varchar 列进行排序

发布于 2024-10-01 02:08:49 字数 527 浏览 0 评论 0原文

我有一个 varchar 列,其中包含混合数据字符串、整数、小数、空白字符串和空值。我想以与 Excel 相同的方式对列进行排序,首先对数字进行排序,然后对字符串进行排序。例如:

  • 1
  • 2
  • 3
  • 3.5
  • 10
  • 11
  • 12
  • alan
  • bob
  • carl
  • (blank/null)
  • (blank/null)

我尝试过执行类似 'ORDER BY my_column+0' 的操作,它可以正确对数字进行排序,但不能对字符串进行排序。我希望有人知道实现这一目标的有效方法。

MartinofD 的建议在很大程度上有效,如果我稍微扩展一下,我就能得到我想要的:

从测试中选择一个 订购依据 a 为 NULL 或 a='', a<>'0' 且 a=0, 一个+0, 一个;

虽然很丑,但我不确定是否有更好的选择。

I have a varchar column with mixed data- strings, integers, decimals, blank strings, and null values. I'd like to sort the column the same way that Excel would, first sorting numbers and then sorting the strings. For example:

  • 1
  • 2
  • 3
  • 3.5
  • 10
  • 11
  • 12
  • alan
  • bob
  • carl
  • (blank/null)
  • (blank/null)

I've tried doing something like 'ORDER BY my_column+0' which sorts the numbers correctly but not the strings. I was hoping someone might know of an efficient way to accomplish this.

MartinofD's suggestion works for the most part and if I expand on it a little bit I can get exactly what I want:

SELECT a FROM test
ORDER BY
a IS NULL OR a='',
a<>'0' AND a=0,
a+0,
a;

Pretty ugly though and I'm not sure if there are any better options.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

浅唱ヾ落雨殇 2024-10-08 02:08:49

这是因为 my_column+0 对于所有字符串 (0) 都是相等的。

只需使用 ORDER BY my_column+0, my_column

mysql> SELECT a FROM test ORDER BY a+0, a;
+-------+
| a     |
+-------+
| NULL  |
| alan  |
| bob   |
| carl  |
| david |
| 1     |
| 2     |
| 3     |
| 3.5   |
| 10    |
| 11    |
| 12    |
+-------+
12 rows in set (0.00 sec)

如果您严格需要数字位于字符串上方,这里有一个解决方案(尽管我不确定这在大表上会多快) :

mysql> SELECT a FROM test ORDER BY (a = CONCAT('', 0+a)) DESC, a+0, a;
+-------+
| a     |
+-------+
| 1     |
| 2     |
| 3     |
| 3.5   |
| 10    |
| 11    |
| 12    |
| alan  |
| bob   |
| carl  |
| david |
| NULL  |
+-------+
12 rows in set (0.00 sec)

That's because my_column+0 is equal for all strings (0).

Just use ORDER BY my_column+0, my_column

mysql> SELECT a FROM test ORDER BY a+0, a;
+-------+
| a     |
+-------+
| NULL  |
| alan  |
| bob   |
| carl  |
| david |
| 1     |
| 2     |
| 3     |
| 3.5   |
| 10    |
| 11    |
| 12    |
+-------+
12 rows in set (0.00 sec)

If you strictly need the numbers to be above the strings, here's a solution (though I'm not sure how quick this will be on big tables):

mysql> SELECT a FROM test ORDER BY (a = CONCAT('', 0+a)) DESC, a+0, a;
+-------+
| a     |
+-------+
| 1     |
| 2     |
| 3     |
| 3.5   |
| 10    |
| 11    |
| 12    |
| alan  |
| bob   |
| carl  |
| david |
| NULL  |
+-------+
12 rows in set (0.00 sec)
太阳哥哥 2024-10-08 02:08:49

这是可行的:

SELECT a FROM test ORDER BY a IS NULL OR a='', a<>'0' AND a=0, a+0, a;

但是任何更高效/优雅的解决方案都会受到欢迎。

This works:

SELECT a FROM test ORDER BY a IS NULL OR a='', a<>'0' AND a=0, a+0, a;

Any more efficient/elegant solution would be welcome however.

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