P
P
Pavel Emtsov2015-12-19 12:41:45
MySQL
Pavel Emtsov, 2015-12-19 12:41:45

How to do correct sorting in MySQL?

There is a table of goods which has an article number field.
Example

a-1
a-2
a-11
b-1
letters-1

I need to sort them both alphabetically and ascending.
Now it's happening like this
a-1
a-11
a-12
a-2

but I would like
a-1
a-2
a-11
a-12

Tell me if there is any easy solution without crutches that come to my mind.
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
C
Cat Anton, 2015-12-19
@emtsov

SELECT * FROM `table` ORDER BY SUBSTRING(`field`, 1, 1), LENGTH(`field`), `field`;

A
Andrey Pavlenko, 2015-12-19
@Akdmeh

www.copterlabs.com/natural-sorting-in-mysql
I think this article should partially help, but it is not a panacea.

I
Ivan, 2015-12-19
@LiguidCool

It would be logical to divide the column by 2.

P
Pavel Emtsov, 2015-12-19
@emtsov

Yes, it only works partially. If I had a- everywhere, it would help.
And so first all the letters in Alvatit with numbers up to 10 go, and then the rest.
c2cfb99d61f24e5896c1559863ae268e.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question