对混合文本和数字的数据进行自然排序(然后是更多文本*有时*)

发布于 2024-11-27 20:43:46 字数 216 浏览 9 评论 0原文

我正在为一个经营牲畜拍卖的人开发一个网站。他将动物输入为 Lot 1、Lot 2、Lot 3、Lot 3a、Lot 4、... Lot 100、... Lot N。由于这是数字和文本的混合,我首先按长度排序( LotName), LotName.* 这一直有效,直到我输入“附加”动物(由末尾的“a”指定)。

我是否可以通过 SQL 直接对数据进行排序?

*在这个网站上找到的,谢谢!

I am working on a site for a guy who runs livestock auctions. He will enter the animals as Lot 1, Lot 2, Lot 3, Lot 3a, Lot 4, ... Lot 100, ... Lot N. Since this is a mix of numbers and text, I was first ordering by Length(LotName), LotName.* That worked until I entered an "add on" animal (specified by the 'a' at the end).

Is there anyway I can get the data to sort right through the SQL?

*Found on this site, thanks!

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

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

发布评论

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

评论(1

爱要勇敢去追 2024-12-04 20:43:46

通过纯SQL?不容易啊!

我们在我的项目代码中做到了这一点,基本上,您应用一个匹配数字或非数字的正则表达式(类似于 ([0-9]+)|([^0-9]+)),将字符串转换为此类游程的元组,将数字游程转换为整数,然后通过成对比较对元组进行排序。例如:

"Lot 1"   -> ("Lot ", 1)
"Lot 2"   -> ("Lot ", 2)
"Lot 3"   -> ("Lot ", 3)
"Lot 3a"  -> ("Lot ", 3, "a")
"Lot 100" -> ("Lot ", 100)

成对比较则 (a) 将整数排序在字符串之前,(b) 按自然顺序对整数排序,以及 (c) 按自然顺序对字符串排序。

如果您能够将正则表达式应用于列进行排序,则可以使用正则表达式将数字“规范化”为固定长度的数字字符串,并用零填充。像这样:

"Lot 1"   -> "Lot 0001"
"Lot 2"   -> "Lot 0002"
"Lot 3"   -> "Lot 0003"
"Lot 3a"  -> "Lot 0003a"
"Lot 100" -> "Lot 0100"

哪个应该正确排序。但是,虽然您可以在 Oracle 和其他一些数据库中执行此操作,但在 MySQL 中,您需要使用用户定义的函数

但是,您可以在数据库之外执行此操作。向批次表中添加一列来存储标准化批次名称,每当您从代码中插入一行时,都会生成标准化表格并存储它。然后您可以使用该列进行排序。

Through pure SQL? Not easy!

We did this in code on my project, and basically, you apply a regular expression that matches runs of digits or non-digits (something like ([0-9]+)|([^0-9]+)), turns a string into a tuple of such runs, converts digit runs into integers, then sorts the tuples with a pairwise comparison. For example:

"Lot 1"   -> ("Lot ", 1)
"Lot 2"   -> ("Lot ", 2)
"Lot 3"   -> ("Lot ", 3)
"Lot 3a"  -> ("Lot ", 3, "a")
"Lot 100" -> ("Lot ", 100)

The pairwise comparison then (a) sorts integers before strings, (b) sorts integers in natural order, and (c) sorts strings in natural order.

If you were able to apply regular expressions to columns for sorting, you could use one to 'normalise' the numbers into digit strings of a fixed length, padded with zeroes. Like this:

"Lot 1"   -> "Lot 0001"
"Lot 2"   -> "Lot 0002"
"Lot 3"   -> "Lot 0003"
"Lot 3a"  -> "Lot 0003a"
"Lot 100" -> "Lot 0100"

Which should sort correctly. However, whilst you can do this in Oracle and some other databases, in MySQL, you need to use a user-defined function.

However, you could do it outside the database. Add a column to the lot table to store the normalised lot name, and whenever you insert a row from your code, generate the normalised form and store it. You can then sort using that column.

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