V
V
Vyacheslav Barsukov2016-02-16 07:43:03
MySQL
Vyacheslav Barsukov, 2016-02-16 07:43:03

How to sort a MYSQL column in this way?

There are tables people of the form
name | number
Ivan | 125
Peter | 250
oleg | 105
nastya | 303
fedora | 0
sleigh | 125
vlad | 0
ruslan | 401
maxims | 236
olya | 148
oksana | 0
you need to sort it in such a way that the sorting would go first by the first digits, then by the last two digits from smallest to largest, and with 0 were at the very end.
It should look like this
name | number
oleg | 105
Ivan | 125
sleigh | 125
Olya | 148
maxims | 236
Peter | 250
nastya | 303
ruslan | 401
fedora | 0
vlad | 0
oksana | 0
and here is the sorting - only by the last 2 digits, zeros at the end
name | number
oleg | 105
ruslan | 401
nastya | 303
Ivan | 125
sleigh | 125
Olya | 148
maxims | 236
Peter | 250
fedoras | 0
vlad | 0
oksana | 0
actually you need to make a mysql query
zeros down so ORDER BY case number when 0 then 2 else 1 end
did one of the options
order by case number when 0 then 2 else 1 end, substring(exp, 1, 2) asc

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yuri, 2016-02-16
@modestguy

order by substring(number, 1, 2), IFNULL(number, 99999999)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question