Reputation: 2556
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
I tried to sort rows with LENGTH()
, CAST()
, SUBSTRING_INDEX()
, but it all failed.
Upvotes: 0
Views: 146
Reputation: 5050
Query 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.) |
Upvotes: 1
Reputation: 3846
Well, this is kinda ugly, but it seems to work on your 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)
Upvotes: 0