Answer the question
In order to leave comments, you need to log in
How to replace identifiers from a table with sequence numbers?
There is a table of the form:
table1
child_id, parent_id, level
table2
id, name
The idea does not come to mind how to replace two fields in the query results without sorting twice - doing it once and remembering, and then simply getting the data from the first query.
The general logic of actions:
1) Take the table2, sort it by name, and assign a serial number to each row
2) Take table 1, for each element get its serial number from the first sort
3) For each parent - get its serial number
4) Sort first by level, then by parent's ordinal, then by child's ordinal
5) filter out values with level 0
Stuck at stage 2.
Is it even possible to do this in a single MySQL query? If not, how to do it in two
SET @num = 0;
CREATE TEMPORARY TABLE `ht_category_sort` (
SELECT
@num := @num + 1 `index`,
t1.`category_id`,
t2.`name`
FROM
ht_category t1
INNER JOIN
ht_category_description t2 ON t1.category_id = t2.category_id AND t2.language_id = 1
ORDER BY
t2.`name` ASC
);
SELECT
t2.`index`,
t3.`index`,
t1.`level`
FROM
ht_category_path t1
LEFT JOIN
ht_category_sort t2 ON t2.category_id = t1.category_id
LEFT JOIN
ht_category_sort t3 ON t3.path_id = t1.category_id
WHERE
t1.`level` = 0
ORDER BY
t1.`level` ASC,
t3.`index` ASC,
t2.`index` ASC
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question