V
V
Vitaly2018-05-19 09:47:02
MySQL
Vitaly, 2018-05-19 09:47:02

How to sort a selection according to a certain algorithm?

Hello everyone, there is a certain selection with LEFT Join that returns, for example, an ID of the form
1,1,1,1,2,2,3,3,3
Can it be reduced to the form
1,2,3,1,2, 3,1,3,1
?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Atlant77, 2018-05-19
@Atlant77

Well, by eye it is 1000 times easier to do through PHP, just sort through the array, select an element from the array, write it to a new array, delete it in the old one and look for a new element, more than the old one, a couple of lines of code.
And in SQL it’s just like trying through UNION and then it’s unlikely (99.99%)

C
catanfa, 2018-05-24
@catanfa

This can be done in SQL using ORDER BY FIELD
https://www.electrictoolbox.com/mysql-order-specif...

S
Sergey Sokolov, 2018-05-24
@sergiks

I see while only a variant with the temporary table. It is necessary to index the same values, give each an increasing index. And then group by these sub-indices:
remake in

1, 0
1, 1
1, 2
1, 3
2, 0
2, 1
3, 0
3, 1
3, 2

and then group by second index and sort by first id

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question