简体   繁体   中英

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. They're should be next MBT1212-8N-3 product.

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:

"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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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