I have the following values in my table:
a 3
a 5
a 7
aa 5
a 10
b 5
With the ORDER BY command I get the following:
a 10
a 3
a 5
a 7
aa 5
b 5
I want the following result:
a 3
a 5
a 7
aa 5
a 10
b 5
Any ideas how I can solve it in my SQL query?
You can use substring_index()
to split data when order:
SELECT
col1
FROM
table1 t
ORDER BY
SUBSTRING_INDEX(col1, ' ', 1),
CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(col1, ' ', 2),' ',- 1) AS UNSIGNED)
However aa
will become after all a
as your desired ordering logic is contradicting with data values in both 'columns', one of them must be leading. This query returns:
a 3
a 5
a 7
a 10
aa 5
b 5
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.