S
S
Sergey Korenevsky2018-06-02 18:53:10
MySQL
Sergey Korenevsky, 2018-06-02 18:53:10

How to sort query by fields left_id, right_id?

In the database of my CMS, the fields ID, left_id, right_id are often found.
How can you sort by such records?
for example table

//Массив(таблица) для добавления записей в базу MYSQL, где порядок полей ['id', 'left_id', 'right_id']
$insert[3] = [3, 0, 7];
$insert[5] = [5, 7, 7];
$insert[7] = [7, 3, 5];
$insert[9] = [9, 5, 5];

Accordingly, the lines should be output in the order 3, 7, 5, 9
But if you sort simply by the left_id field, then it is sorted in the order 3, 7, 9, 5
As you can see, the last 2 lines have the reverse order

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2018-06-02
@Dier_Sergio_Great

Try to express the rule for your sorting in Russian first. Standard sorting by two fields sounds like "sort in ascending order of the first field, additionally sort rows with the same value in the first field in ascending order of the second field". What exactly do you want to get?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question