My MySQL query gets motorcycle model names sorted by name
SELECT * FROM models ORDER BY name
i.e.:
[
'1200 Superenduro'
'250 ETC Enduro, silver',
'350 ETC 2014',
'450 ETC 2014',
'50 EC-X',
'690 ETC 2014',
'RS 1200 Superenduro'
]
The first word in the string is usually a number with engine capacity and I'd like to sort by this capacity. The expected output should be:
[
'50 EC-X',
'250 ETC Enduro, silver',
'350 ETC 2014',
'450 ETC 2014',
'690 ETC 2014',
'1200 Superenduro'
'RS 1200 Superenduro'
]
I tried to ORDER BY
the first word, but looks like it doesn't really work:
SELECT * FROM models ORDER BY SUBSTRING_INDEX(name, " ", 1)
. How to achieve proper sorting? I use MySQL 5.7. I may manipulate the output in PHP.