简体   繁体   English

自然排序查询在 mysql 中不起作用

[英]Natural Sorting query is not working in mysql

Sorry for my english.对不起我的英语不好。 I want to sort, in natural sorting, my records.我想以自然排序方式对我的记录进行排序。 This are some:这是一些:

MBT44-2N-1
MBT44-4N-3
MBT44-8N-1
MBT44-6N-3
MBT66-6N-1
MBT86-8N-3
MBT88-12N-1
MBT88-12N-3
MBT88-4N-1
MBT88-4N-3
MBT88-6N-1
MBT88-6N-3
MBT88-8N-1
MBT88-8N-3
MBT1212-12N-1
MBT1212-12N-3
MBT1212-4N-1
MBT1212-4N-3
MBT1212-8N-1
MBT1212-8N-3
MBT1616-6N-1
MBT1616-8N-3
MBT2020-8N-1

I already had the order using the length and column name order like this:我已经有了使用长度和列名顺序的顺序,如下所示:

LENGTH({$wpdb->posts}.post_title) ASC, {$wpdb->posts}.post_title ASC

But the results is not working that we want.但结果并没有达到我们想要的效果。 This is a short query result:这是一个简短的查询结果:

MBT1212-8N-1
MBT1212-8N-3
MBT1616-4N-1
MBT1616-8N-1
MBT1616-8N-3
MBT2020-8N-1
*MBT1212-12N-1*
*MBT1212-12N-3*

Note the "MBT1212-12N-1" and "MBT1212-12N-3" on the bottom of the results.请注意结果底部的“MBT1212-12N-1”和“MBT1212-12N-3”。 They're should be next MBT1212-8N-3 product.它们应该是下一个 MBT1212-8N-3 产品。

This is part of my query but It works by parts:这是我的查询的一部分,但它按部分工作:

SELECT post_title FROM `wp_posts` 
WHERE post_type = 'product' AND post_title LIKE 'MBT%' 
order BY length(SUBSTRING_INDEX(`post_title`,'-',1)) ASC,
  post_title ASC, 
  length(post_title) asc, 
  post_title asc;

This is another aproach:这是另一种方法:

SELECT post_title FROM `wp_posts` 
WHERE post_type = 'product' AND post_title LIKE 'MBT%' 
order BY length(SUBSTRING_INDEX(`post_title`,'-',1)) ASC,
  CAST(SUBSTRING_INDEX(`post_title`,'-',1) as UNSIGNED) ASC,
  post_title ASC, 
  length(post_title) asc, 
  post_title asc;

I try to split the record by "hyphen" and sort by each one part but it works a little odd.我尝试用“连字符”分割记录并按每个部分排序,但它有点奇怪。

I appreciate all your help.我感谢您的所有帮助。 Thanks in advance.提前致谢。

Perhaps a clumsy way but it works fine for me.也许是一种笨拙的方式,但对我来说效果很好。 Try this:尝试这个:

SELECT post_title,
  RPAD(
  CONCAT(
  LPAD(SUBSTRING_INDEX(SUBSTRING_INDEX(post_title, "-", 1), "-", -1),12," "),
  LPAD(SUBSTRING_INDEX(SUBSTRING_INDEX(post_title, "-", 2), "-", -1),12," "),
  LPAD(SUBSTRING_INDEX(SUBSTRING_INDEX(post_title, "-", 3), "-", -1),12," "),
  LPAD(SUBSTRING_INDEX(SUBSTRING_INDEX(post_title, "-", 4), "-", -1),12," ")
  ),48,"0"
  ) AS C
  FROM mytable
ORDER BY C ASC

The basic idea is to transform each section in the string that is divided with a separator, in your case "-", to equally long substrings padded with blanks.基本思想是将字符串中用分隔符分隔的每个部分(在您的情况下为“-”)转换为用空格填充的等长子字符串。

In your example the posts will be transformed to a 48 character, 4 levels times 12 characters, string as follows:在您的示例中,帖子将转换为 48 个字符、4 个级别乘以 12 个字符的字符串,如下所示:

"MBT44-2N-1"     -->     "MBT44       2N         1           1           "
"MBT44-4N-3"     -->     "MBT44       4N         3           3           "
"MBT44-8N-1"      -->    "MBT44       8N         1           1           "
"MBT44-6N-3"     -->     "MBT44       6N         3           3           "

This strings are then used to sort the result.然后使用此字符串对结果进行排序。 If you have more levels you just add more LPAD lines as above and increase the value for the total length of the sorting string.如果您有更多级别,您只需添加更多如上所述的 LPAD 行并增加排序字符串总长度的值。

This presume that you know the maximum length of the substrings and also the maximum number of levels divided by "-" in you string.这假定您知道子字符串的最大长度以及字符串中除以“-”的最大级别数。 But this can be handeled with variables that you set after examined the strings.但这可以通过您在检查字符串后设置的变量来处理。

You can also have different separators for different levels but only if they are on the same place for all values to sort.您还可以为不同级别使用不同的分隔符,但前提是它们位于同一位置以对所有值进行排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM