简体   繁体   English

MySQL对文本和数字进行自然排序

[英]MySQL natural sorting with text and numbers

I have table with item name, for example: 我有带有项目名称的表,例如:

...
Cheburashka with detailed ear 10g. (O.Box 5 pcs.)
Cheburashka with detailed ear 12g. (O.Box 5 pcs.)
Cheburashka with detailed ear 4g. (O.Box 5 pcs.)
Skittle with two swivels 20g. (O.Box 5 pcs.)
Skittle with two swivels 6g. (O.Box 5 pcs.)
...

And I need: 我需要:

...
Cheburashka with detailed ear 4g. (O.Box 5 pcs.)
Cheburashka with detailed ear 10g. (O.Box 5 pcs.)
Cheburashka with detailed ear 12g. (O.Box 5 pcs.)
Skittle with two swivels 6g. (O.Box 5 pcs.)
Skittle with two swivels 20g. (O.Box 5 pcs.)
...

etc, and I need to sort the rows alphabetically, and with value numbers - weight. 等等,我需要按字母顺序对行进行排序,并使用数值-重量。

SQL Fiddle link - http://sqlfiddle.com/#!2/4cbd8/1 SQL小提琴链接- http://sqlfiddle.com/#!2/4cbd8/1

I tried to sort rows with LENGTH() , CAST() , SUBSTRING_INDEX() , but it all failed. 我尝试使用LENGTH()CAST()SUBSTRING_INDEX()对行进行排序,但是都失败了。

Query 1 : 查询1

SELECT item_name
FROM items 
ORDER BY 
reverse(mid(
reverse(mid(replace(replace(replace(replace(replace(item_name,',',''),' . ','. '),'r. ','g. '),'d. ','g. '),' g ','g. '),1,instr(replace(replace(replace(replace(item_name,' . ','. '),'r. ','g. '),'d. ','g. '),' g ','g. '), 'g. (')-1)),
instr(reverse(mid(replace(replace(replace(replace(replace(item_name,',',''),' . ','. '),'r. ','g. '),'d. ','g. '),' g ','g. '),1,instr(replace(replace(replace(replace(item_name,' . ','. '),'r. ','g. '),'d. ','g. '),' g ','g. '), 'g. (')-1)),' ')
)),
cast(reverse(mid(
reverse(mid(replace(replace(replace(replace(replace(item_name,',',''),' . ','. '),'r. ','g. '),'d. ','g. '),' g ','g. '),1,instr(replace(replace(replace(replace(item_name,' . ','. '),'r. ','g. '),'d. ','g. '),' g ','g. '), 'g. (')-1)),1,  
instr(reverse(mid(replace(replace(replace(replace(replace(item_name,',',''),' . ','. '),'r. ','g. '),'d. ','g. '),' g ','g. '),1,instr(replace(replace(replace(replace(item_name,' . ','. '),'r. ','g. '),'d. ','g. '),' g ','g. '), 'g. (')-1)),' ')
)) as signed)

Results : 结果

|                                                ITEM_NAME |
|----------------------------------------------------------|
|   Load Cheburashka with detailed ear 4g. ( O.Box 5 pcs.) |
|   Load Cheburashka with detailed ear 6d. ( O.Box 5 pcs.) |
|   Load Cheburashka with detailed ear 8g. ( O.Box 5 pcs.) |
|  Load Cheburashka with detailed ear 10g. ( O.Box 5 pcs.) |
|  Load Cheburashka with detailed ear 12g. ( O.Box 5 pcs.) |
| Load Cheburashka with detailed ear 14g . ( O.Box 5 pcs.) |
| Load Cheburashka with detailed ear 16g . ( O.Box 5 pcs.) |
| Load Cheburashka with detailed ear 18g . ( O.Box 5 pcs.) |
| Load Cheburashka with detailed ear 20g . ( O.Box 5 pcs.) |
| Load Cheburashka with detailed ear 22g . ( O.Box 3 pcs.) |
| Load Cheburashka with detailed ear 24g . ( O.Box 3 pcs.) |
| Load Cheburashka with detailed ear 26g . ( O.Box 3 pcs.) |
| Load Cheburashka with detailed ear 28g . ( O.Box 3 pcs.) |
| Load Cheburashka with detailed ear 32g . ( O.Box 3 pcs.) |
| Load Cheburashka with detailed ear 36g . ( O.Box 3 pcs.) |
| Load Cheburashka with detailed ear 40g . ( O.Box 3 pcs.) |
|                  Load Cone with dope 3d. ( O.Box 5 pcs.) |
|                  Load Cone with dope 4g. ( O.Box 5 pcs.) |
|                  Load Cone with dope 6d. ( O.Box 5 pcs.) |
|                 Load Cone with dope, 8g. ( O.Box 5 pcs.) |
|                 Load Cone with dope 10g. ( O.Box 5 pcs.) |
|                 Load Cone with dope 12g. ( O.Box 5 pcs.) |
|                Load Cone with dope 13g . ( O.Box 5 pcs.) |
|                Load Cone with dope 15g . ( O.Box 5 pcs.) |
|                Load Cone with dope 18g . ( O.Box 5 pcs.) |
|               Load Ovsinka with dope 3d. ( O.Box 5 pcs.) |
|              Load Ovsinka with dope, 7g. ( O.Box 5 pcs.) |
|              Load Ovsinka with dope 10g. ( O.Box 5 pcs.) |
|             Load Ovsinka with dope 14g . ( O.Box 5 pcs.) |
|             Load Ovsinka with dope 18g . ( O.Box 5 pcs.) |
|               Load Skittle with dope 6d. ( O.Box 5 pcs.) |
|              Load Skittle with dope, 9g. ( O.Box 5 pcs.) |
|             Load Skittle with dope 14g . ( O.Box 5 pcs.) |
|             Load Skittle with dope 16g . ( O.Box 5 pcs.) |
|             Load Skittle with dope 20g . ( O.Box 5 pcs.) |
|        Load Skittle with two swivels 6d. ( O.Box 5 pcs.) |
|        Load Skittle with two swivels 9g. ( O.Box 5 pcs.) |
|      Load Skittle with two swivels 14g . ( O.Box 5 pcs.) |
|      Load Skittle with two swivels 16g . ( O.Box 5 pcs.) |
|      Load Skittle with two swivels 20g . ( O.Box 5 pcs.) |
|             Oliva cargo with dope, 3.5 g ( O.Box 5 pcs.) |
|               Oliva cargo with dope, 7g. ( O.Box 5 pcs.) |
|            Oliva cargo with dope, 10.5 g ( O.Box 5 pcs.) |
|              Oliva cargo with dope 14g . ( O.Box 5 pcs.) |
|                Thumb load with dope, 2r. ( O.Box 5 pcs.) |
|                 Thumb load with dope 3d. ( O.Box 5 pcs.) |
|                Thumb load with dope, 7g. ( O.Box 5 pcs.) |
|                Thumb load with dope 10g. ( O.Box 5 pcs.) |

Well, this is kinda ugly, but it seems to work on your sqlfiddle: 好吧,这有点丑陋,但是它似乎可以在您的sqlfiddle上运行:

SELECT item_name

FROM items ORDER BY 
item_name,
cast(substring(item_name,LEAST(
    if (Locate('0',item_name) >0,Locate('0',item_name),999),
    if (Locate('1',item_name) >0,Locate('1',item_name),999),
    if (Locate('2',item_name) >0,Locate('2',item_name),999),
    if (Locate('3',item_name) >0,Locate('3',item_name),999),
    if (Locate('4',item_name) >0,Locate('4',item_name),999),
    if (Locate('5',item_name) >0,Locate('5',item_name),999),
    if (Locate('6',item_name) >0,Locate('6',item_name),999),
    if (Locate('7',item_name) >0,Locate('7',item_name),999),
    if (Locate('8',item_name) >0,Locate('8',item_name),999),
    if (Locate('9',item_name) >0,Locate('9',item_name),999)
  )) as unsigned)

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

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