R
R
RMV19832014-08-18 20:13:23
MySQL
RMV1983, 2014-08-18 20:13:23

Is complex sorting with a condition possible in MySQL?

There is a field by which you need to sort, but not quite in order, i.e. it should look something like this: 1;1;1;2;4;3;2;4;3;5;5;…
1 - at the beginning, and then - as it will. those. display a certain value "up".
And the rest of the data that would be sorted later and by other fields?
At the moment I have full sorting by several fields separated by commas in ORDER - and it turns out like this:
1 | a
1 | Z
2 | a
2 | Z
3 | a
3 | Z
And you need this:
1 | a
1 | Z
2 | a
3 | a
2 | Z
3 | Z
(Both fields are varchar, utf-8)
I know the variant with the introduction of one more field, according to which sorting is going on. A complex SELECT is also not needed. Interested - is it possible to make one SQL query, maybe there is some kind of standard workpiece?
MySQL 5

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
WebSpider, 2014-08-18
@RMV1983

Look towards ORDER BY FIELD(...)

A
Alexey Skahin, 2014-08-20
@pihel

You can also write CASE WHEN in ORDER

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question