0

我有带有项目名称的表,例如:

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

我需要:

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

等,我需要按字母顺序对行进行排序,并使用值数字 - 重量。

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

我尝试使用 , , 对行进行排序LENGTH()CAST()SUBSTRING_INDEX()都失败了。

4

2 回答 2

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)

结果

|                                                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.) |
于 2013-10-07T14:31:01.900 回答
0

好吧,这有点难看,但它似乎适用于您的 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)
于 2013-10-07T13:13:12.433 回答