Altering the order of a hierarchical result generated by a Recursive CTE?

孤者浪人 提交于 2020-08-06 04:29:09

问题


I am using MySQL and I am wondering if it is possible to alter the order of the result generated by recursive CTE.

My Table has these columns:

|----------|----------|----------|----------|
|   ID     | parentID |  title   |   Sort   |
|----------|----------|----------|----------|
|    1     |    null  |  Maria   |     1    |
|    2     |    1     |  John    |     2    |
|    3     |    2     |  Maria   |     3    |
|    4     |    1     |  Anthony |     1    |
|    5     |    4     |  XXX     |     1    |
|    6     |    4     |  ...     |     2    |
|    7     |    2     |  ...     |     2    |
|    8     |    2     |  ...     |     1    |
|    9     |    1     |  ...     |     3    |

I use the following query (We don't take into account the sort column)

   WITH RECURSIVE cte AS
    (
      SELECT ID, parentID, title, 0 AS depth, CAST(ID AS CHAR(200)) AS path
        FROM categories WHERE ID = 1
      UNION ALL
      SELECT c.ID, c.parentID, c.title, cte.depth + 1, CONCAT(cte.path, ',', c.ID) 
        FROM categories c 
        JOIN cte ON cte.parentID = c.ID
        WHERE FIND_IN_SET(c.ID, cte.path)=0
    )
    SELECT * FROM cte ORDER BY cte.path

Next is the hierarchical result (in IDs) that we get from the above query. We completely ignore the sort column.

Hierarchy Depth
1   2   3
|   |   |
IDs
1
    2
        3
        7
        8
    4
        5
        6
    9

What I desire is a query that takes account the sort column and creates the following order. From what you see IDs with 4,2,9 have sort numbers 1,2,3 respectively and that order is taken into account in the result in depth level 2 and similarly in all Depth levels.

Hierarchy Depth
1   2   3
|   |   |
IDs
1
    4
        5
        6
    2
        8
        7
        3
    9

Looking for an edit to my query to achieve the above result.


回答1:


I think you can construct the sorting path based on the sort priorities. Unfortunately, these seem to be repeated on different rows, so I am also going to include the original id:

WITH RECURSIVE cte AS (
      SELECT ID, parentID, title, 0 AS depth, CAST(ID AS CHAR(200)) AS path,
             CONCAT(sort, '-', id) as sort_path
      FROM categories
      WHERE ID = 2
      UNION ALL
      SELECT c.ID, c.parentID, c.title, cte.depth + 1, CONCAT(cte.path, ',', c.ID) ,
             CONCAT(cte.sort_path, c.sort, '-', c.id, ',') as sort_path
      FROM categories c JOIN
           cte
           ON cte.parentID = c.ID
      WHERE FIND_IN_SET(c.ID, cte.path) = 0
    )
SELECT *
FROM cte
ORDER BY sort_path;

Here is a db<>fiddle. With your sample data, this only goes one level deep, so it really doesn't show whether or not this works. Also, this assumes that ids and sorting priorities are never more than one character -- as does your query, by the way.



来源:https://stackoverflow.com/questions/62612106/altering-the-order-of-a-hierarchical-result-generated-by-a-recursive-cte

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!