If the name
value pattern is fixed as brandX modelY Type
, then following solution should work.
Use ORDER BY FIELD
on splitting the name
into desired words order.
Example:
select name from (
select name
, @s1:=substring( name, locate( ' ', name )+1 ) s1 -- ModelY Premium/Normal/Standard
, @s2:=substring_index( @s1, ' ', 1 ) s2 -- ModelY
, @s3:=substring_index( @s1, ' ', -1 ) s3 -- Premium/Normal/Standard
from table_name
) segregated_name_results
order by
field( s2, 'Model2', 'Model1', 'Model3' )
, field( s3, 'Premium', 'Normal', 'Standard' )
;
+------------------------+
| name |
+------------------------+
| Brand1 Model2 Premium |
| Brand1 Model2 Normal |
| Brand1 Model2 Standard |
| Brand1 Model1 Premium |
| Brand1 Model1 Normal |
| Brand1 Model1 Standard |
| Brand1 Model3 Premium |
| Brand1 Model3 Normal |
| Brand1 Model3 Standard |
+------------------------+
Reference Values on Sub-query:
select name
, @s1:=substring( name, locate( ' ', name )+1 ) s1
, @s2:=substring_index( @s1, ' ', 1 ) s2
, @s3:=substring_index( @s1, ' ', -1 ) s3
from brand_models
+------------------------+-----------------+--------+----------+
| name | s1 | s2 | s3 |
+------------------------+-----------------+--------+----------+
| Brand1 Model1 Premium | Model1 Premium | Model1 | Premium |
| Brand1 Model1 Standard | Model1 Standard | Model1 | Standard |
| Brand1 Model1 Normal | Model1 Normal | Model1 | Normal |
| Brand1 Model2 Normal | Model2 Normal | Model2 | Normal |
| Brand1 Model2 Premium | Model2 Premium | Model2 | Premium |
| Brand1 Model2 Standard | Model2 Standard | Model2 | Standard |
| Brand1 Model3 Normal | Model3 Normal | Model3 | Normal |
| Brand1 Model3 Premium | Model3 Premium | Model3 | Premium |
| Brand1 Model3 Standard | Model3 Standard | Model3 | Standard |
+------------------------+-----------------+--------+----------+
Demo @ MySQL 5.5.32 Fiddle