SQL 查询获取分层树文件夹结构

SQL query to get hierarchical tree folder structure

我有如下表格

file_table

f_id  file_name
 21     abc.xml
 13     xyz.xml

folder_table

f_id   f_name
 15     Main
 21     Sub
 13     Sub2

group_table

parent  child
 21      13
 15      21

In file_table, file_name "xyz.xml" 及其对应的 f_id 是 13(sub2) 基于这个 f_id 我想检查它的父级来自 group_table 的节点,即 21(sub)。然后检查 group_table 中的前一个父节点是否有对应的父节点,即 15(Main) 等等。最后检查此父节点是否存在于 child 中,如果不存在则为根节点。 13 -> 21 -> 15
我想像下面这样对 select 数据编写查询。

f_name           file_name
Main/Sub/Sub2     xyz.xml
Main/Sub          abc.xml

您可以使用递归 common table expression (CTE) in your query to get all the directories in a file's path. In your main query, you can (ab)use a SELECT ... FOR XML 子查询将所有这些目录连接成一个完整路径。

WITH CTE (f_id, depth, f_id_depth) AS (
  --Anchor statement
  --(This provides the starting result set of the CTE.)
  SELECT DISTINCT
    f_id,
    1,
    f_id
  FROM
    file_table
  UNION ALL
  --Recursive statement
  --(Note this contains the CTE itself in the FROM-clause.)
  --(This statement gets executed repeatedly until it returns no rows anymore.)
  --(Each time, its results are added to the CTE's result set.)
  SELECT
    CTE.f_id,
    CTE.depth + 1,
    F.f_id
  FROM
    CTE
    INNER JOIN group_table AS G ON G.child = CTE.f_id_depth
    INNER JOIN folder_table AS F ON F.f_id = G.parent
)
SELECT
  --Use a SELECT ... FOR XML subquery to concatenate the folder names (each prefixed with a slash) in a single string.
  --Additionally, wrap the subquery in a STUFF function to remove the leading slash.
  STUFF((SELECT '/' + FF.f_name
         FROM CTE INNER JOIN folder_table AS FF ON FF.f_id = CTE.f_id_depth
         WHERE CTE.f_id = F.f_id
         ORDER BY CTE.depth DESC
         FOR XML PATH('')), 1, 1, '') AS f_name,
  F.file_name
FROM
  file_table AS F;