{"id":374869,"date":"2024-05-21T06:11:00","date_gmt":"2024-05-21T06:11:00","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=374869"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=374869","title":{"rendered":"<span>\u041e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0447\u0430\u0441\u0442\u0438 \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0438 \u0432 SQL \u0437\u0430\u043f\u0440\u043e\u0441\u0435<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<details class=\"spoiler\">\n<summary>\u042d\u0442\u043e \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435<\/summary>\n<div class=\"spoiler__content\">\n<p><a href=\"https:\/\/habr.com\/ru\/articles\/810687\/\" rel=\"noopener noreferrer nofollow\">\u0441\u0442\u0430\u0442\u044c\u0438<\/a>, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u0432\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0430\u043c\u0438 SQL \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 MySQL \u0438 SQLite<\/p>\n<p>SQL \u0437\u0430\u043f\u0440\u043e\u0441 \u0438\u0437 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0447\u0430\u0441\u0442\u0438 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0435\u0442 \u0432\u0441\u044e \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u044e, \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u043a\u043e\u0440\u043d\u0435\u0432\u044b\u0445 \u0443\u0437\u043b\u043e\u0432 (\u043d\u0435 \u0438\u043c\u0435\u044e\u0449\u0438\u0445 &#171;\u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f&#187;)<\/p>\n<\/div>\n<\/details>\n<h3>\u0418\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 &#8212; \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0435\u043c \u0447\u0430\u0441\u0442\u044c \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0438<\/h3>\n<p>\u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0432\u044b\u0432\u0435\u0441\u0442\u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u0447\u0430\u0441\u0442\u044c \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0438, \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0445 \u0443\u0437\u043b\u043e\u0432, \u043d\u0435 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043a\u043e\u0440\u043d\u0435\u0432\u044b\u0445. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u044f \u043e\u0431\u044a\u0435\u043c\u043d\u0430\u044f, \u044d\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u0442 \u0441\u043e\u043a\u0440\u0430\u0442\u0438\u0442\u044c \u043e\u0431\u044a\u0435\u043c \u0432\u044b\u0432\u043e\u0434\u0430, \u043b\u0438\u0431\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u043e\u0442\u0431\u0440\u043e\u0441\u0438\u0442\u044c \u043d\u0435\u043d\u0443\u0436\u043d\u044b\u0435 \u0443\u0440\u043e\u0432\u043d\u0438.<\/p>\n<p>\u0412 \u0446\u0435\u043b\u043e\u043c \u0444\u043e\u0440\u043c\u0443\u043b\u0438\u0440\u043e\u0432\u043a\u0430 \u0437\u0430\u0434\u0430\u0447\u0438 \u043d\u0435 \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u0441\u044f, \u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u0441\u044f \u043e\u0434\u043d\u043e \u0443\u0441\u043b\u043e\u0432\u0438\u0435:<\/p>\n<ul>\n<li>\n<p>\u0417\u0430\u043f\u0440\u043e\u0441 \u0434\u043e\u043b\u0436\u0435\u043d \u0432\u044b\u0432\u043e\u0434\u0438\u0442\u044c \u0432\u0441\u044e \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u044e, \u043b\u0438\u0431\u043e \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0437\u0430\u0434\u0430\u043d \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0439 \u0443\u0437\u0435\u043b (\u0438\u043b\u0438 \u0441\u043f\u0438\u0441\u043e\u043a \u0443\u0437\u043b\u043e\u0432) \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0438, \u0434\u043b\u044f \u0432\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0435\u0435 \u0447\u0430\u0441\u0442\u0438, \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0449\u0435\u0439\u0441\u044f \u0441 \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0443\u0437\u043b\u0430<\/p>\n<\/li>\n<\/ul>\n<p>\u041f\u043e\u0434\u0445\u043e\u0434 \u043a \u0440\u0435\u0448\u0435\u043d\u0438\u044e \u0432 \u0446\u0435\u043b\u043e\u043c \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f, \u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u0441\u044f \u0435\u0449\u0435 \u043e\u0434\u043d\u043e CTE, \u0438 \u0432 \u0442\u0440\u0438 \u0431\u0443\u0434\u0443\u0442 \u0432\u043d\u0435\u0441\u0435\u043d\u044b \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f:<\/p>\n<ol>\n<li>\n<p><strong>Mapping<\/strong>: \u0431\u0435\u0437 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439<\/p>\n<\/li>\n<li>\n<p><strong>RootNodes<\/strong>: \u043d\u043e\u0432\u043e\u0435 CTE \u0434\u043b\u044f \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0432\u0441\u0435\u0445 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0445 \u0443\u0437\u043b\u043e\u0432. \u0418\u043c\u0435\u0435\u0442 2 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430 &#8212; \u0431\u0430\u0437\u043e\u0432\u044b\u0439, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0438\u0437\u0432\u043b\u0435\u043a\u0430\u044e\u0442\u0441\u044f \u0432\u0441\u0435 \u043a\u043e\u0440\u043d\u0435\u0432\u044b\u0435 \u0443\u0437\u043b\u044b, \u043d\u0435 \u0438\u043c\u0435\u044e\u0449\u0438\u0435 &#171;\u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f&#187;, \u0438 \u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u043d\u044b\u0439, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0437\u0430\u0434\u0430\u0435\u0442\u0441\u044f \u0441\u043f\u0438\u0441\u043e\u043a \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0445 \u0443\u0437\u043b\u043e\u0432. \u0422\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0438\u0437 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u0432 \u0437\u0430\u043f\u0440\u043e\u0441\u0435 (\u0432\u0442\u043e\u0440\u043e\u0439 \u043d\u0443\u0436\u043d\u043e \u0437\u0430\u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c)<\/p>\n<\/li>\n<li>\n<p><strong>Levels<\/strong>: \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e\u0435 CTE, \u0441\u043e\u0435\u0434\u0438\u043d\u044f\u044e\u0449\u0435\u0435 \u043d\u0430 \u043f\u0435\u0440\u0432\u043e\u043c \u0443\u0440\u043e\u0432\u043d\u0435 <code>Mapping<\/code> \u0438 <code>RootNodes<\/code> (\u0447\u0435\u0440\u0435\u0437 \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0435\u0435 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435), \u0438 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u0441\u043e\u0431\u0438\u0440\u0430\u044e\u0449\u0435\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0418\u0414 \u00ab\u0440\u043e\u0434\u0438\u0442\u0435\u043b\u0435\u0439\u00bb \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0443\u0437\u043b\u0430 <code>parents<\/code>, \u043f\u043e\u043b\u043d\u043e\u0435 \u0438\u043c\u044f \u0443\u0437\u043b\u0430 <code>full_path<\/code> \u0438 \u0443\u0440\u043e\u0432\u0435\u043d\u044c \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0438 <code>node_level<\/code><\/p>\n<\/li>\n<li>\n<p><strong>Branches<\/strong>: \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u043d\u043e\u0435 CTE, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0443\u0437\u043b\u043e\u0432, \u043d\u0435 \u044f\u0432\u043b\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u043a\u043e\u0440\u043d\u0435\u0432\u044b\u043c\u0438\/\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u043c\u0438, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u0432\u0438\u0434 \u00ab\u0432\u0435\u0442\u0432\u0438\u00bb, \u0430 \u0442\u0430\u043a \u0436\u0435  \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u044e\u0442\u0441\u044f \u00ab\u0441\u043a\u0432\u043e\u0437\u043d\u044b\u0435\u00bb \u0432\u0435\u0442\u0432\u0438 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 \u0434\u043b\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0438\u0445  \u00ab\u043f\u043e\u0442\u043e\u043c\u043a\u0430\u043c\u0438\u00bb. \u0412 CTE \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043e\u043a\u043d\u043e <code>WindowByParents<\/code> \u0441 \u043f\u0430\u0440\u0442\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043f\u043e \u043f\u043e\u043b\u044e <code>Levels.parents<\/code>, \u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>LAST_VALUE(id) OVER WindowByParents<\/code> \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0441\u043e\u0431\u044b\u0439 \u0432\u0438\u0434 \u00ab\u0432\u0435\u0442\u0432\u0438\u00bb \u0434\u043b\u044f \u00ab\u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0445\u00bb \u0443\u0437\u043b\u043e\u0432 \u0443\u0440\u043e\u0432\u043d\u044f. \u0414\u043b\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043a\u043e\u0440\u043d\u0435\u0432\u044b\u0445\/\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0445 \u0443\u0437\u043b\u043e\u0432 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043b\u0435\u0432\u043e\u0435 \u0432\u043d\u0435\u0448\u043d\u0435\u0435 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 <code>Levels<\/code> \u0438 <code>RootNodes<\/code><\/p>\n<\/li>\n<li>\n<p><strong>Tree<\/strong>: \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e\u0435 CTE, \u0441\u043e\u0435\u0434\u0438\u043d\u044f\u044e\u0449\u0435\u0435 \u043d\u0430 \u043f\u0435\u0440\u0432\u043e\u043c \u0443\u0440\u043e\u0432\u043d\u0435 <code>Mapping<\/code> \u0438 <code>RootNodes<\/code> (\u0447\u0435\u0440\u0435\u0437 \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0435\u0435 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435), \u0438 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u0441\u043e\u0431\u0438\u0440\u0430\u044e\u0449\u0435\u0435 \u0432\u0441\u0435 \u0441\u043a\u0432\u043e\u0437\u043d\u044b\u0435 \u00ab\u0432\u0435\u0442\u043a\u0438\u00bb \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u0435\u0439 \u0443\u0437\u043b\u0430 \u0432 \u043f\u043e\u043b\u0435 <code>all_through<\/code><\/p>\n<\/li>\n<li>\n<p><strong>FineTree<\/strong>: \u0431\u0435\u0437 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439<\/p>\n<\/li>\n<\/ol>\n<h4>\u0418\u0437\u043c\u0435\u043d\u0435\u043d\u043d\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 (\u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0432\u0441\u0435 CTE)<\/h4>\n<details class=\"spoiler\">\n<summary>Mapping \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0444\u0430\u0439\u043b\u043e\u0432\u043e\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0438\u0437 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0438<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"sql\">with recursive  Mapping as ( select  id as node_id,  parent_directory_id as parent_node_id, name as node_name from Files ),  RootNodes as ( select node_id as root_node_id from Mapping where -- Exactly one line below should be uncommented parent_node_id is null-- Uncomment to build from root(s) -- node_id in (1)-- Uncomment to add node_id(s) into the brackets ),  Levels as ( select node_id, parent_node_id, node_name, cast(parent_node_id as char(2000)) as parents, cast(node_name as char(2000)) as full_path, 0 as node_level from Mapping inner join RootNodes on node_id = root_node_id  union  select  Mapping.node_id,  Mapping.parent_node_id, Mapping.node_name, concat(coalesce(concat(prev.parents, '-'), ''), cast(Mapping.parent_node_id as char)), concat_ws(' ', prev.full_path, Mapping.node_name), prev.node_level + 1 from  Levels as prev inner join Mapping on Mapping.parent_node_id = prev.node_id ),  Branches as ( select node_id, parent_node_id, node_name, parents, full_path, node_level, case when root_node_id is null then case when node_id = last_value(node_id) over WindowByParents then '\u2514\u2500\u2500 ' else '\u251c\u2500\u2500 ' end else '' end as node_branch, case when root_node_id is null then case when node_id = last_value(node_id) over WindowByParents then '    ' else '\u2502   ' end else '' end as branch_through from Levels left join RootNodes on node_id = root_node_id window WindowByParents as ( partition by parents order by node_name rows between current row and unbounded following ) order by full_path ),  Tree as ( select node_id, parent_node_id, node_name, parents, full_path, node_level, node_branch, cast(branch_through as char(2000)) as all_through from Branches inner join RootNodes on node_id = root_node_id  union  select  Branches.node_id, Branches.parent_node_id, Branches.node_name, Branches.parents, Branches.full_path, Branches.node_level, Branches.node_branch, concat(prev.all_through, Branches.branch_through) from  Tree as prev inner join Branches on Branches.parent_node_id = prev.node_id ),  FineTree as ( select tr.node_id, tr.parent_node_id, tr.node_name, tr.parents, tr.full_path, tr.node_level, concat(coalesce(parent.all_through, ''), tr.node_branch, tr.node_name) as fine_tree from Tree as tr left join Tree as parent on parent.node_id = tr.parent_node_id order by tr.full_path )  select fine_tree, node_id from FineTree ;<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u041f\u0440\u0438\u0448\u043b\u043e\u0441\u044c \u043e\u0442\u0441\u0442\u0443\u043f\u0438\u0442\u044c \u043e\u0442 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0430 \u0438\u0437\u043e\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0441\u0442\u0438 CTE \u043f\u043e &#171;\u0443\u0440\u043e\u0432\u043d\u044f\u043c&#187; &#8212; <code>RootNodes<\/code> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0442\u0440\u0438\u0436\u0434\u044b \u0432 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f\u0445 \u0432 \u0440\u0430\u0437\u043d\u044b\u0445 CTE. \u041d\u043e, \u0442\u0430\u043a \u043a\u0430\u043a \u043e\u043d \u043d\u0435 \u0438\u0437\u0432\u043b\u0435\u043a\u0430\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0438 \u0434\u043b\u044f \u0432\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438, \u0430 \u0441\u043b\u0443\u0436\u0438\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0430, \u0431\u0443\u0434\u0443 \u0441\u0447\u0438\u0442\u0430\u0442\u044c, \u0447\u0442\u043e \u044d\u0442\u043e \u043d\u0435 \u0441\u0447\u0438\u0442\u0430\u0435\u0442\u0441\u044f )<\/p>\n<details class=\"spoiler\">\n<summary>\u0421 \u0431\u0430\u0437\u043e\u0432\u044b\u043c \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u043c RootNodes&#8230;<\/summary>\n<div class=\"spoiler__content\">\n<figure class=\"float full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/f71\/7e5\/2f3\/f717e52f3c2d822901841f67630b402a.png\" alt=\"SQLite\" title=\"SQLite\" width=\"545\" height=\"439\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/f71\/7e5\/2f3\/f717e52f3c2d822901841f67630b402a.png\"\/><\/p>\n<div><figcaption>SQLite<\/figcaption><\/div>\n<\/figure>\n<p>&#8230; \u0437\u0430\u043f\u0440\u043e\u0441 \u043e\u0442\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u0442\u0430\u043a \u0436\u0435, \u043a\u0430\u043a \u0432 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0447\u0430\u0441\u0442\u0438 \u0441\u0442\u0430\u0442\u044c\u0438, \u0438 \u0432 MySQL 8.2, \u0438 \u0432 SQLite 3.44.1<\/p>\n<\/div>\n<\/details>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u043e\u0432\u0435\u0440\u044e \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0447\u0430\u0441\u0442\u0438 \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0438, \u043f\u043e\u0434\u0441\u0442\u0430\u0432\u0438\u0432 \u0418\u0414 3 \u0438 11 \u0432 \u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u043d\u044b\u0439 <code>RootNodes<\/code>:<\/p>\n<pre><code class=\"sql\">RootNodes as ( select node_id as root_node_id from Mapping where -- Keep uncommented exactly one line below  -- parent_node_id is null-- Uncomment to build from root(s) node_id in (3, 11)-- Uncomment to add node_id(s) into the brackets ),<\/code><\/pre>\n<figure class=\"float\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/340\/f69\/498\/340f69498f2ad5ad62d707436f66685a.png\" alt=\"MySQL\" title=\"MySQL\" width=\"453\" height=\"312\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/340\/f69\/498\/340f69498f2ad5ad62d707436f66685a.png\"\/><\/p>\n<div><figcaption>MySQL<\/figcaption><\/div>\n<\/figure>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0441 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u044f\u043c: \u0432\u044b\u0432\u0435\u0434\u0435\u043d\u0430 \u0447\u0430\u0441\u0442\u044c \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0438, \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0443\u0437\u043b\u043e\u0432 \u0441 \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u0432 <code>RootNodes<\/code> \u0418\u0414<\/p>\n<h2>\u0415\u0449\u0451 \u043e\u0434\u043d\u0430 \u0444\u0430\u0439\u043b\u043e\u0432\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430<\/h2>\n<p>\u0412\u043e\u0437\u044c\u043c\u0443 \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0438\u0437 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u043d\u043e\u0439 \u0432 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 <a href=\"https:\/\/stepik.org\/lesson\/1195940\/step\/2?unit=1230007\" rel=\"noopener noreferrer nofollow\">\u0442\u043e\u0439 \u0436\u0435 \u0441\u0430\u043c\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438<\/a> \u0438\u0437 <a href=\"https:\/\/stepik.org\/course\/113918\/info\" rel=\"noopener noreferrer nofollow\">\u0442\u043e\u0433\u043e \u0441\u0430\u043c\u043e\u0433\u043e \u043a\u0443\u0440\u0441\u0430 \u043f\u043e SQL<\/a> \u043d\u0430 stepik.org &#8212; \u043d\u0430\u0434\u0435\u044e\u0441\u044c \u043d\u0430 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435 \u0441\u043d\u0438\u0441\u0445\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0430\u0432\u0442\u043e\u0440\u043e\u0432, \u043d\u043e \u043f\u043e\u0441\u0442\u0430\u0440\u0430\u044e\u0441\u044c \u0431\u043e\u043b\u044c\u0448\u0435 \u0435\u0439 \u043d\u0435 \u0437\u043b\u043e\u0443\u043f\u043e\u0442\u0440\u0435\u0431\u043b\u044f\u0442\u044c, \u0434\u043b\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0447\u0430\u0441\u0442\u0438 \u0441\u043e\u0437\u0434\u0430\u043c \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u044e \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e ))<\/p>\n<details class=\"spoiler\">\n<summary>\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0442\u0430\u0431\u043b\u0438\u0446\u044b Files \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0430, \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u0442\u0440\u043e\u043a \u0432\u044b\u0440\u043e\u0441\u043b\u043e \u043f\u0440\u0438 \u0442\u043e\u0439 \u0436\u0435 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0441\u0442\u0438<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"sql\">DROP TABLE IF EXISTS Files; CREATE TABLE Files (     id                  INT,     name                VARCHAR(40),     parent_directory_id INT );  INSERT INTO Files (id, name, parent_directory_id) VALUES  (1, 'desktop', null),         (2, 'another_folder', 1),         (3, 'FOLDER', 2),         (4, 'code.jpeg', 3),         (5, 'player10.json', 3),         (6, 'player11.json', 3),         (7, 'player5.json', 3),         (8, 'player6.json', 3),         (9, 'stepik.png', 3),         (10, 'FOLDER_HERE', 2),         (11, 'Crying All the Time.mp3', 10),         (12, 'player1.json', 10),         (13, 'player14.json', 10),         (14, 'player39.json', 2),         (15, 'player45.json', 2),         (16, 'player46.json', 2),         (17, 'python.pdf', 2),         (18, 'shopping_list.txt', 2),         (19, 'folder', 1),         (20, 'player19.json', 19),         (21, 'player20.json', 19),         (22, 'player21.json', 19),         (23, 'player42.json', 19),         (24, 'player47.json', 19),         (25, 'player48.json', 19),         (26, 'player49.json', 19);<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u0418\u0435\u0440\u0430\u0440\u0445\u0438\u044f \u0432 \u0431\u0430\u0437\u043e\u0432\u043e\u043c \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0435 <code>RootNodes<\/code> \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u044f\u043c (MySQL):<\/p>\n<pre><code class=\"bash\">fine_tree                          |node_id| -----------------------------------+-------+ desktop                            |      1| \u251c\u2500\u2500 another_folder                 |      2| \u2502   \u251c\u2500\u2500 FOLDER                     |      3| \u2502   \u2502   \u251c\u2500\u2500 code.jpeg              |      4| \u2502   \u2502   \u251c\u2500\u2500 player10.json          |      5| \u2502   \u2502   \u251c\u2500\u2500 player11.json          |      6| \u2502   \u2502   \u251c\u2500\u2500 player5.json           |      7| \u2502   \u2502   \u251c\u2500\u2500 player6.json           |      8| \u2502   \u2502   \u2514\u2500\u2500 stepik.png             |      9| \u2502   \u251c\u2500\u2500 FOLDER_HERE                |     10| \u2502   \u2502   \u251c\u2500\u2500 Crying All the Time.mp3|     11| \u2502   \u2502   \u251c\u2500\u2500 player1.json           |     12| \u2502   \u2502   \u2514\u2500\u2500 player14.json          |     13| \u2502   \u251c\u2500\u2500 player39.json              |     14| \u2502   \u251c\u2500\u2500 player45.json              |     15| \u2502   \u251c\u2500\u2500 player46.json              |     16| \u2502   \u251c\u2500\u2500 python.pdf                 |     17| \u2502   \u2514\u2500\u2500 shopping_list.txt          |     18| \u2514\u2500\u2500 folder                         |     19|     \u251c\u2500\u2500 player19.json              |     20|     \u251c\u2500\u2500 player20.json              |     21|     \u251c\u2500\u2500 player21.json              |     22|     \u251c\u2500\u2500 player42.json              |     23|     \u251c\u2500\u2500 player47.json              |     24|     \u251c\u2500\u2500 player48.json              |     25|     \u2514\u2500\u2500 player49.json              |     26|<\/code><\/pre>\n<p>\u0422\u0430\u043a \u0436\u0435 \u0447\u0430\u0441\u0442\u044c \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0438:<\/p>\n<pre><code class=\"sql\">RootNodes as ( select node_id as root_node_id from Mapping where -- Keep uncommented exactly one line below  -- parent_node_id is null-- Uncomment to build from root(s) node_id in (3, 10, 17)-- Uncomment to add node_id(s) into the brackets ),<\/code><\/pre>\n<figure class=\"float\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/75c\/1c0\/25c\/75c1c025cc17f78598a498d65b1438ff.png\" alt=\"SQLite\" title=\"SQLite\" width=\"440\" height=\"367\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/75c\/1c0\/25c\/75c1c025cc17f78598a498d65b1438ff.png\"\/><\/p>\n<div><figcaption>SQLite<\/figcaption><\/div>\n<\/figure>\n<p><a class=\"anchor\" name=\"slash_breaks_hierarchy\" id=\"slash_breaks_hierarchy\"><\/a><\/p>\n<p>\u0415\u0441\u0442\u044c \u043d\u044e\u0430\u043d\u0441 &#8212; \u0432 \u044d\u0442\u043e\u0439 \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0438 \u043d\u0430 \u043e\u0434\u043d\u043e\u043c \u0443\u0440\u043e\u0432\u043d\u0435 \u043d\u0430\u0445\u043e\u0434\u044f\u0442\u0441\u044f &#171;\u043f\u0430\u043f\u043a\u0438&#187; \u0441 \u043f\u043e\u0445\u043e\u0436\u0438\u043c\u0438 \u0438\u043c\u0435\u043d\u0430\u043c\u0438 <code>\"FOLDER\"<\/code> \u0438 <code>\"FOLDER_HERE\"<\/code>. \u0415\u0441\u043b\u0438 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u0445 \u0438\u043c\u0435\u043d\u0430 \u0432 \u0441\u043f\u0438\u0441\u043a\u0435 \u043f\u043e\u0442\u043e\u043c\u043a\u043e\u0432 \u0443\u0437\u043b\u0430 <code>\"another_folder\"<\/code>, \u043e\u043d\u0438 \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0430\u0442\u0441\u044f \u0432 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435, \u043d\u043e \u0435\u0441\u043b\u0438 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u0441\u0435\u0445 \u0443\u0437\u043b\u043e\u0432 \u043f\u043e \u043f\u043e\u043b\u043d\u044b\u043c \u043f\u0443\u0442\u044f\u043c, \u043a\u0430\u043a \u0432 CTE <code>FineTree<\/code> \u0434\u043b\u044f \u0444\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f, \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u0432\u0438\u0441\u0435\u0442\u044c \u043e\u0442 \u0441\u0438\u043c\u0432\u043e\u043b\u0430-\u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u0435\u043b\u044f, \u0447\u0435\u0440\u0435\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0431\u0440\u0430\u043d\u0430 \u0441\u0442\u0440\u043e\u043a\u0430 <code>full_path<\/code>. \u0415\u0441\u043b\u0438 \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0431\u0435\u043b <code>\" \"<\/code> \u043a\u043e\u0441\u043e\u0439 \u0447\u0435\u0440\u0442\u043e\u0439 <code>\"\/\"<\/code>, \u043f\u0440\u0438\u0432\u044b\u0447\u043d\u043e\u0439 \u0434\u043b\u044f \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u043f\u0443\u0442\u0435\u0439 \u0432 \u0444\u0430\u0439\u043b\u043e\u0432\u043e\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0435, \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0431\u0443\u0434\u0435\u0442 \u043d\u0435\u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u044b\u043c:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/98b\/e62\/928\/98be629281271e50e40d9b6228bb2671.png\" alt=\"\u0423\u0437\u0435\u043b FOLDER &quot;\u043f\u043e\u0442\u0435\u0440\u044f\u043b&quot; \u0441\u0432\u043e\u0438\u0445 \u043f\u043e\u0442\u043e\u043c\u043a\u043e\u0432, \u0442.\u043a. FOLDER_HERE\/ \u0438\u0434\u0435\u0442 \u0440\u0430\u043d\u044c\u0448\u0435 FOLDER\/\" title=\"\u0423\u0437\u0435\u043b FOLDER &quot;\u043f\u043e\u0442\u0435\u0440\u044f\u043b&quot; \u0441\u0432\u043e\u0438\u0445 \u043f\u043e\u0442\u043e\u043c\u043a\u043e\u0432, \u0442.\u043a. FOLDER_HERE\/ \u0438\u0434\u0435\u0442 \u0440\u0430\u043d\u044c\u0448\u0435 FOLDER\/\" width=\"771\" height=\"278\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/98b\/e62\/928\/98be629281271e50e40d9b6228bb2671.png\"\/><\/p>\n<div><figcaption>\u0423\u0437\u0435\u043b FOLDER &#171;\u043f\u043e\u0442\u0435\u0440\u044f\u043b&#187; \u0441\u0432\u043e\u0438\u0445 \u043f\u043e\u0442\u043e\u043c\u043a\u043e\u0432, \u0442.\u043a. FOLDER_HERE\/ \u0438\u0434\u0435\u0442 \u0440\u0430\u043d\u044c\u0448\u0435 FOLDER\/<\/figcaption><\/div>\n<\/figure>\n<h2>\u0414\u0440\u0443\u0433\u0430\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u0430 &#8212; \u043a\u0430\u0442\u0435\u0433\u043e\u0440\u0438\u0438 \u0442\u043e\u0432\u0430\u0440\u043e\u0432<\/h2>\n<p>\u041f\u0440\u0438\u0432\u0435\u0434\u0443 \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0441\u043e \u0441\u0445\u043e\u0436\u0435\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b, \u043d\u043e \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u043d\u044b\u043c\u0438 \u0438\u043c\u0435\u043d\u0430\u043c\u0438,<\/p>\n<details class=\"spoiler\">\n<summary>\u043a\u043e\u043c\u0443 \u044d\u0442\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"sql\">DROP TABLE IF EXISTS Categories; CREATE TABLE Categories (     category_id         INT,     parent_category_id INT,     name                VARCHAR(40) );  INSERT INTO Categories (category_id, parent_category_id, name) VALUES (1, NULL, '\u0422\u043e\u0432\u0430\u0440\u044b \u0434\u043b\u044f \u0434\u043e\u043c\u0430'), (2, NULL, '\u0426\u0438\u0444\u0440\u043e\u0432\u0430\u044f \u0442\u0435\u0445\u043d\u0438\u043a\u0430'), (3, 1, '\u0411\u044b\u0442\u043e\u0432\u0430\u044f \u0442\u0435\u0445\u043d\u0438\u043a\u0430'), (4, 2, '\u041d\u043e\u0443\u0442\u0431\u0443\u043a\u0438 \u0438 \u0430\u043a\u0441\u0435\u0441\u0441\u0443\u0430\u0440\u044b'), (5, 2, '\u0424\u043e\u0442\u043e\u0430\u043f\u043f\u0430\u0440\u0430\u0442\u044b'), (6, 2, '\u0418\u0433\u0440\u043e\u0432\u044b\u0435 \u043a\u043e\u043d\u0441\u043e\u043b\u0438'), (7, 2, '\u0410\u0443\u0434\u0438\u043e\u0442\u0435\u0445\u043d\u0438\u043a\u0430'), (8, 2, '\u0421\u043e\u0442\u043e\u0432\u044b\u0435 \u0442\u0435\u043b\u0435\u0444\u043e\u043d\u044b'), (9, 4, '\u041d\u043e\u0443\u0442\u0431\u0443\u043a\u0438'), (10, 4, '\u0420\u044e\u043a\u0437\u0430\u043a\u0438');<\/code><\/pre>\n<p>\u0414\u043b\u044f \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043d\u0430 \u043d\u043e\u0432\u044b\u0439 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a \u043d\u0443\u0436\u043d\u043e \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c <code>Mapping<\/code>:<\/p>\n<pre><code class=\"sql\">with recursive  Mapping as ( select  category_idas node_id,  parent_category_idas parent_node_id, name as node_name from Categories ),<\/code><\/pre>\n<figure class=\"float\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/0da\/fc6\/7b8\/0dafc67b82aba5547ef178edd5f143da.png\" alt=\"SQLite\" title=\"SQLite\" width=\"414\" height=\"317\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/0da\/fc6\/7b8\/0dafc67b82aba5547ef178edd5f143da.png\"\/><\/p>\n<div><figcaption>SQLite<\/figcaption><\/div>\n<\/figure>\n<p>\u0417\u0430\u043f\u0440\u043e\u0441 \u0432 \u0431\u0430\u0437\u043e\u0432\u043e\u043c \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0435 \u043e\u0442\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e, \u0445\u043e\u0442\u044f \u044d\u0442\u043e \u0443\u0436\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043b\u043e \u0431\u044b\u0442\u044c \u0441\u044e\u0440\u043f\u0440\u0438\u0437\u043e\u043c ))<\/p>\n<\/div>\n<\/details>\n<h2>\u0412\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0435 \u043e\u0448\u0438\u0431\u043a\u0438<\/h2>\n<p>\u0422\u0430\u043a \u043a\u0430\u043a \u0432 \u0437\u0430\u043f\u0440\u043e\u0441 \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0418\u0414 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0445 \u0443\u0437\u043b\u043e\u0432, \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0438\u0445 \u0441\u043e\u0447\u0435\u0442\u0430\u043d\u0438\u044f \u0441\u043f\u0440\u043e\u0432\u043e\u0446\u0438\u0440\u0443\u044e\u0442 \u043e\u0448\u0438\u0431\u043a\u0438. \u0414\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u0431\u0443\u0434\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u044e\u044e &#171;\u043a\u043e\u0440\u043e\u0442\u043a\u0443\u044e&#187; \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u044e<\/p>\n<h4>\u041f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f \u0438 \u043f\u043e\u0442\u043e\u043c\u043a\u0430 \u043a\u0430\u043a \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0445 \u0443\u0437\u043b\u043e\u0432<\/h4>\n<p>\u0422\u0430\u043a\u043e\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441\u043f\u0440\u043e\u0432\u043e\u0446\u0438\u0440\u0443\u0435\u0442 \u0434\u0443\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0443\u0437\u043b\u043e\u0432 \u0438 \u0438\u0441\u043a\u0430\u0436\u0435\u043d\u0438\u0435 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0438<\/p>\n<pre><code class=\"sql\">RootNodes as ( select node_id as root_node_id from Mapping where -- Keep uncommented exactly one line below  -- parent_node_id is null-- Uncomment to build from root(s) node_id in (2, 4)-- Uncomment to add node_id(s) into the brackets ),<\/code><\/pre>\n<figure class=\"float\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/cc0\/188\/d4c\/cc0188d4ce90991fb7e9bd4293c0d771.png\" alt=\"SQLite\" title=\"SQLite\" width=\"371\" height=\"436\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/cc0\/188\/d4c\/cc0188d4ce90991fb7e9bd4293c0d771.png\"\/><\/p>\n<div><figcaption>SQLite<\/figcaption><\/div>\n<\/figure>\n<p>\u0421\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u0430 &#171;\u0421\u043f\u0438\u0441\u043a\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438&#187; \u043d\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0440\u043e\u0441\u0442\u044b\u043c \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u043c \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0438\u0442\u044c \u0442\u0430\u043a\u0443\u044e \u043a\u043e\u043b\u043b\u0438\u0437\u0438\u044e \u0432 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u0445, \u0435\u0441\u043b\u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u044b\u0435 \u0443\u0437\u043b\u044b \u043d\u0435 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043f\u0440\u044f\u043c\u044b\u043c\u0438 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u0435\u043c \u0438 \u043f\u043e\u0442\u043e\u043c\u043a\u043e\u043c, \u0447\u0442\u043e \u043d\u0435 \u043f\u043e\u0434\u043e\u0439\u0434\u0435\u0442 \u0432 \u043e\u0431\u0449\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435.<br \/><em>\u0418 <\/em><code>distinct<\/code><em> \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044e \u0442\u0430\u043a \u0436\u0435 \u0438\u0441\u043f\u0440\u0430\u0432\u0438\u0442 \u043b\u0438\u0448\u044c \u043e\u0442\u0447\u0430\u0441\u0442\u0438, \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u044e \u0443\u0431\u0435\u0434\u0438\u0442\u044c\u0441\u044f \u0432 \u044d\u0442\u043e\u043c \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e<\/em><\/p>\n<h4>\u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u044f, \u043e\u0448\u0438\u0431\u043a\u0430 \u043f\u0440\u0438 \u043f\u043e\u043f\u044b\u0442\u043a\u0435 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f<\/h4>\n<p>\u041c\u043e\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c &#171;\u0437\u0430\u0446\u0438\u043a\u043b\u0435\u043d\u043d\u0443\u044e&#187; \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u044e, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0442\u0430\u043a:<\/p>\n<pre><code class=\"sql\">DROP TABLE IF EXISTS Categories; CREATE TABLE Categories (     category_id         INT,     parent_category_id INT,     name                VARCHAR(40) );  INSERT INTO Categories (category_id, parent_category_id, name) VALUES (1, NULL, '\u0422\u043e\u0432\u0430\u0440\u044b \u0434\u043b\u044f \u0434\u043e\u043c\u0430'),   (2, 9, '\u0426\u0438\u0444\u0440\u043e\u0432\u0430\u044f \u0442\u0435\u0445\u043d\u0438\u043a\u0430'),    -- \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043f\u043e\u0442\u043e\u043c\u043e\u043a \"\u041d\u043e\u0443\u0442\u0431\u0443\u043a\u0438\" (3, 1, '\u0411\u044b\u0442\u043e\u0432\u0430\u044f \u0442\u0435\u0445\u043d\u0438\u043a\u0430'), (4, 2, '\u041d\u043e\u0443\u0442\u0431\u0443\u043a\u0438 \u0438 \u0430\u043a\u0441\u0435\u0441\u0441\u0443\u0430\u0440\u044b'), (5, 2, '\u0424\u043e\u0442\u043e\u0430\u043f\u043f\u0430\u0440\u0430\u0442\u044b'), (6, 2, '\u0418\u0433\u0440\u043e\u0432\u044b\u0435 \u043a\u043e\u043d\u0441\u043e\u043b\u0438'), (7, 2, '\u0410\u0443\u0434\u0438\u043e\u0442\u0435\u0445\u043d\u0438\u043a\u0430'), (8, 2, '\u0421\u043e\u0442\u043e\u0432\u044b\u0435 \u0442\u0435\u043b\u0435\u0444\u043e\u043d\u044b'), (9, 4, '\u041d\u043e\u0443\u0442\u0431\u0443\u043a\u0438'), (10, 4, '\u0420\u044e\u043a\u0437\u0430\u043a\u0438');<\/code><\/pre>\n<p>\u041f\u0440\u0438 \u043f\u043e\u043f\u044b\u0442\u043a\u0435 \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f \u0442\u0430\u043a\u043e\u0439 \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0438 \u043e\u0442 \u0443\u0437\u043b\u0430 \u0441 \u0418\u0414 = 2 \u043f\u0440\u043e\u0438\u0437\u043e\u0439\u0434\u0435\u0442 \u043b\u0438\u0431\u043e \u043f\u0435\u0440\u0435\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u044f <code>full_path<\/code>:, \u043b\u0438\u0431\u043e \u043f\u0435\u0440\u0435\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0441\u0442\u0438 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438, \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0439 \u0421\u0423\u0411\u0414 \u0438 \u0435\u0435 \u043d\u0430\u0441\u0442\u0440\u043e\u0435\u043a<\/p>\n<pre><code class=\"sql\">RootNodes as ( select node_id as root_node_id from Mapping where -- Keep uncommented exactly one line below  -- parent_node_id is null-- Uncomment to build from root(s)   node_id in (2)-- Uncomment to add node_id(s) into the brackets ),<\/code><\/pre>\n<blockquote>\n<p>\u0412 MySQL 8.2:<\/p>\n<p>SQL Error [3636] [HY000]: Recursive query aborted after 1001 iterations. Try increasing <a class=\"mention\" href=\"\/users\/cte_max_recursion_depth\">@cte_max_recursion_depth<\/a> to a larger value.<\/p>\n<p><em>SQLite \u043f\u0440\u043e\u0441\u0442\u043e \u0437\u0430\u0432\u0438\u0441\u0430\u0435\u0442<\/em><\/p>\n<\/blockquote>\n<p>\u041f\u0440\u0438 \u0431\u0430\u0437\u043e\u0432\u043e\u043c \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0438 &#171;\u043e\u0442 \u043a\u043e\u0440\u043d\u044f&#187; \u044d\u0442\u043e &#171;\u043a\u043e\u043b\u044c\u0446\u043e \u0443\u0437\u043b\u043e\u0432&#187; \u043f\u0440\u043e\u0441\u0442\u043e \u043d\u0435 \u043f\u043e\u043f\u0430\u0434\u0435\u0442 \u0432 \u0432\u044b\u0431\u043e\u0440\u043a\u0443, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0431\u043e\u0440\u043e\u0442\u044c\u0441\u044f \u0441 \u044d\u0442\u043e\u0439 \u043e\u0448\u0438\u0431\u043a\u043e\u0439 \u044f \u043d\u0435 \u0432\u0438\u0436\u0443 \u043e\u0441\u043e\u0431\u043e\u0433\u043e \u0441\u043c\u044b\u0441\u043b\u0430 &#8212; \u0435\u0435 \u043c\u043e\u0436\u043d\u043e \u0432\u043e\u0441\u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0441\u0442\u0438, \u0441\u043e\u0437\u043d\u0430\u0442\u0435\u043b\u044c\u043d\u043e &#171;\u0437\u0430\u043a\u043e\u043b\u044c\u0446\u0435\u0432\u0430\u0432&#187; \u0447\u0430\u0441\u0442\u044c \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0438 \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0432 \u043e\u0434\u0438\u043d \u0438\u043b\u0438 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0443\u0437\u043b\u043e\u0432 \u043a\u043e\u043b\u044c\u0446\u0430 \u043a\u0430\u043a \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 \u0432 <code>RootNodes<\/code><\/p>\n<p><em>\u041f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435 \u0441\u043b\u0435\u0434\u0443\u0435\u0442 &#8212; \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u044e &#171;\u0431\u043e\u043b\u044c\u0448\u0438\u0435&#187; \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0438 \u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0432 PostrgreSQL<\/em><\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><\/p>\n<div class=\"tm-article-poll-container\"><!--[--><\/p>\n<div class=\"tm-article-poll tm-article-poll_variant-bordered\">\n<div class=\"tm-notice tm-notice_positive tm-article-poll__notice\"><!----><\/p>\n<div class=\"tm-notice__inner\"><!----><\/p>\n<div class=\"tm-notice__content\" data-test-id=\"notice-content\"><!--[--><span>\u0422\u043e\u043b\u044c\u043a\u043e \u0437\u0430\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0438 \u043c\u043e\u0433\u0443\u0442 \u0443\u0447\u0430\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0432 \u043e\u043f\u0440\u043e\u0441\u0435. <a rel=\"nofollow\" href=\"\/kek\/v1\/auth\/habrahabr\/?back=\/ru\/articles\/810855\/&#038;hl=ru\">\u0412\u043e\u0439\u0434\u0438\u0442\u0435<\/a>, \u043f\u043e\u0436\u0430\u043b\u0443\u0439\u0441\u0442\u0430.<\/span><!--]--><\/div>\n<\/div>\n<\/div>\n<p><!--[--><\/p>\n<div class=\"tm-article-poll__header\">\u041f\u0440\u0438\u0445\u043e\u0434\u0438\u043b\u043e\u0441\u044c \u043b\u0438 \u0432\u0430\u043c \u0432\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0432 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445?<\/div>\n<div class=\"tm-article-poll__answers\"><!--[--><\/p>\n<div class=\"tm-article-poll__answer\">\n<div class=\"tm-article-poll__answer-data\"><span class=\"tm-article-poll__answer-percent\">11.11% <\/span><span class=\"tm-article-poll__answer-label\">\u0414\u0430, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0433\u043e\u0442\u043e\u0432\u044b\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442<\/span><span class=\"tm-article-poll__answer-votes\">2<\/span><\/div>\n<div class=\"tm-article-poll__answer-bar\">\n<div class=\"tm-article-poll__answer-progress\" style=\"width: 11.11%\"><\/div>\n<\/div>\n<\/div>\n<div class=\"tm-article-poll__answer\">\n<div class=\"tm-article-poll__answer-data\"><span class=\"tm-article-poll__answer-percent\">44.44% <\/span><span class=\"tm-article-poll__answer-label\">\u0414\u0430, \u043d\u0430\u043f\u0438\u0441\u0430\u0432 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443 \u043d\u0430 \u00ab\u043d\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u043e\u043c\u00bb \u044f\u0437\u044b\u043a\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f<\/span><span class=\"tm-article-poll__answer-votes\">8<\/span><\/div>\n<div class=\"tm-article-poll__answer-bar\">\n<div class=\"tm-article-poll__answer-progress\" style=\"width: 44.44%\"><\/div>\n<\/div>\n<\/div>\n<div class=\"tm-article-poll__answer\">\n<div class=\"tm-article-poll__answer-data\"><span class=\"tm-article-poll__answer-percent tm-article-poll__answer-percent_winning\">44.44% <\/span><span class=\"tm-article-poll__answer-label\">\u0414\u0430, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0432 SQL<\/span><span class=\"tm-article-poll__answer-votes\">8<\/span><\/div>\n<div class=\"tm-article-poll__answer-bar\">\n<div class=\"tm-article-poll__answer-progress tm-article-poll__answer-progress_winning\" style=\"width: 44.44%\"><\/div>\n<\/div>\n<\/div>\n<div class=\"tm-article-poll__answer\">\n<div class=\"tm-article-poll__answer-data\"><span class=\"tm-article-poll__answer-percent\">11.11% <\/span><span class=\"tm-article-poll__answer-label\">\u0414\u0430, \u0432\u043e\u043e\u0431\u0440\u0430\u0437\u0438\u0432 \u0435\u0435 \u0432 \u0433\u043e\u043b\u043e\u0432\u0435<\/span><span class=\"tm-article-poll__answer-votes\">2<\/span><\/div>\n<div class=\"tm-article-poll__answer-bar\">\n<div class=\"tm-article-poll__answer-progress\" style=\"width: 11.11%\"><\/div>\n<\/div>\n<\/div>\n<div class=\"tm-article-poll__answer\">\n<div class=\"tm-article-poll__answer-data\"><span class=\"tm-article-poll__answer-percent\">16.67% <\/span><span class=\"tm-article-poll__answer-label\">\u041d\u0435\u0442, \u044d\u0442\u043e \u043d\u0438\u043a\u043e\u043c\u0443 \u043d\u0435 \u043d\u0443\u0436\u043d\u043e, \u043a\u0430\u043a \u043c\u0438\u043d\u0438\u043c\u0443\u043c \u043c\u043d\u0435<\/span><span class=\"tm-article-poll__answer-votes\">3<\/span><\/div>\n<div class=\"tm-article-poll__answer-bar\">\n<div class=\"tm-article-poll__answer-progress\" style=\"width: 16.67%\"><\/div>\n<\/div>\n<\/div>\n<p><!--]--><\/div>\n<div class=\"tm-article-poll__stats\"> \u041f\u0440\u043e\u0433\u043e\u043b\u043e\u0441\u043e\u0432\u0430\u043b\u0438 18 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439.   \u0412\u043e\u0437\u0434\u0435\u0440\u0436\u0430\u043b\u0438\u0441\u044c 3 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f. <\/div>\n<p><!--]--><\/div>\n<p><!--]--><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/810855\/\"> https:\/\/habr.com\/ru\/articles\/810855\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<details class=\"spoiler\">\n<summary>\u042d\u0442\u043e \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435<\/summary>\n<div class=\"spoiler__content\">\n<p><a href=\"https:\/\/habr.com\/ru\/articles\/810687\/\" rel=\"noopener noreferrer nofollow\">\u0441\u0442\u0430\u0442\u044c\u0438<\/a>, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u0432\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0430\u043c\u0438 SQL \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 MySQL \u0438 SQLite<\/p>\n<p>SQL \u0437\u0430\u043f\u0440\u043e\u0441 \u0438\u0437 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0447\u0430\u0441\u0442\u0438 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0435\u0442 \u0432\u0441\u044e \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u044e, \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u043a\u043e\u0440\u043d\u0435\u0432\u044b\u0445 \u0443\u0437\u043b\u043e\u0432 (\u043d\u0435 \u0438\u043c\u0435\u044e\u0449\u0438\u0445 &#171;\u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f&#187;)<\/p>\n<\/div>\n<\/details>\n<h3>\u0418\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 &#8212; \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0435\u043c \u0447\u0430\u0441\u0442\u044c \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0438<\/h3>\n<p>\u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0432\u044b\u0432\u0435\u0441\u0442\u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u0447\u0430\u0441\u0442\u044c \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0438, \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0445 \u0443\u0437\u043b\u043e\u0432, \u043d\u0435 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043a\u043e\u0440\u043d\u0435\u0432\u044b\u0445. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u044f \u043e\u0431\u044a\u0435\u043c\u043d\u0430\u044f, \u044d\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u0442 \u0441\u043e\u043a\u0440\u0430\u0442\u0438\u0442\u044c \u043e\u0431\u044a\u0435\u043c \u0432\u044b\u0432\u043e\u0434\u0430, \u043b\u0438\u0431\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u043e\u0442\u0431\u0440\u043e\u0441\u0438\u0442\u044c \u043d\u0435\u043d\u0443\u0436\u043d\u044b\u0435 \u0443\u0440\u043e\u0432\u043d\u0438.<\/p>\n<p>\u0412 \u0446\u0435\u043b\u043e\u043c \u0444\u043e\u0440\u043c\u0443\u043b\u0438\u0440\u043e\u0432\u043a\u0430 \u0437\u0430\u0434\u0430\u0447\u0438 \u043d\u0435 \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u0441\u044f, \u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u0441\u044f \u043e\u0434\u043d\u043e \u0443\u0441\u043b\u043e\u0432\u0438\u0435:<\/p>\n<ul>\n<li>\n<p>\u0417\u0430\u043f\u0440\u043e\u0441 \u0434\u043e\u043b\u0436\u0435\u043d \u0432\u044b\u0432\u043e\u0434\u0438\u0442\u044c \u0432\u0441\u044e \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u044e, \u043b\u0438\u0431\u043e \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0437\u0430\u0434\u0430\u043d \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0439 \u0443\u0437\u0435\u043b (\u0438\u043b\u0438 \u0441\u043f\u0438\u0441\u043e\u043a \u0443\u0437\u043b\u043e\u0432) \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0438, \u0434\u043b\u044f \u0432\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0435\u0435 \u0447\u0430\u0441\u0442\u0438, \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0449\u0435\u0439\u0441\u044f \u0441 \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0443\u0437\u043b\u0430<\/p>\n<\/li>\n<\/ul>\n<p>\u041f\u043e\u0434\u0445\u043e\u0434 \u043a \u0440\u0435\u0448\u0435\u043d\u0438\u044e \u0432 \u0446\u0435\u043b\u043e\u043c \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f, \u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u0441\u044f \u0435\u0449\u0435 \u043e\u0434\u043d\u043e CTE, \u0438 \u0432 \u0442\u0440\u0438 \u0431\u0443\u0434\u0443\u0442 \u0432\u043d\u0435\u0441\u0435\u043d\u044b \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f:<\/p>\n<ol>\n<li>\n<p><strong>Mapping<\/strong>: \u0431\u0435\u0437 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439<\/p>\n<\/li>\n<li>\n<p><strong>RootNodes<\/strong>: \u043d\u043e\u0432\u043e\u0435 CTE \u0434\u043b\u044f \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0432\u0441\u0435\u0445 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0445 \u0443\u0437\u043b\u043e\u0432. \u0418\u043c\u0435\u0435\u0442 2 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430 &#8212; \u0431\u0430\u0437\u043e\u0432\u044b\u0439, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0438\u0437\u0432\u043b\u0435\u043a\u0430\u044e\u0442\u0441\u044f \u0432\u0441\u0435 \u043a\u043e\u0440\u043d\u0435\u0432\u044b\u0435 \u0443\u0437\u043b\u044b, \u043d\u0435 \u0438\u043c\u0435\u044e\u0449\u0438\u0435 &#171;\u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f&#187;, \u0438 \u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u043d\u044b\u0439, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0437\u0430\u0434\u0430\u0435\u0442\u0441\u044f \u0441\u043f\u0438\u0441\u043e\u043a \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0445 \u0443\u0437\u043b\u043e\u0432. \u0422\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0438\u0437 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u0432 \u0437\u0430\u043f\u0440\u043e\u0441\u0435 (\u0432\u0442\u043e\u0440\u043e\u0439 \u043d\u0443\u0436\u043d\u043e \u0437\u0430\u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c)<\/p>\n<\/li>\n<li>\n<p><strong>Levels<\/strong>: \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e\u0435 CTE, \u0441\u043e\u0435\u0434\u0438\u043d\u044f\u044e\u0449\u0435\u0435 \u043d\u0430 \u043f\u0435\u0440\u0432\u043e\u043c \u0443\u0440\u043e\u0432\u043d\u0435 <code>Mapping<\/code> \u0438 <code>RootNodes<\/code> (\u0447\u0435\u0440\u0435\u0437 \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0435\u0435 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435), \u0438 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u0441\u043e\u0431\u0438\u0440\u0430\u044e\u0449\u0435\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0418\u0414 \u00ab\u0440\u043e\u0434\u0438\u0442\u0435\u043b\u0435\u0439\u00bb \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0443\u0437\u043b\u0430 <code>parents<\/code>, \u043f\u043e\u043b\u043d\u043e\u0435 \u0438\u043c\u044f \u0443\u0437\u043b\u0430 <code>full_path<\/code> \u0438 \u0443\u0440\u043e\u0432\u0435\u043d\u044c \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0438 <code>node_level<\/code><\/p>\n<\/li>\n<li>\n<p><strong>Branches<\/strong>: \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u043d\u043e\u0435 CTE, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0443\u0437\u043b\u043e\u0432, \u043d\u0435 \u044f\u0432\u043b\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u043a\u043e\u0440\u043d\u0435\u0432\u044b\u043c\u0438\/\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u043c\u0438, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u0432\u0438\u0434 \u00ab\u0432\u0435\u0442\u0432\u0438\u00bb, \u0430 \u0442\u0430\u043a \u0436\u0435  \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u044e\u0442\u0441\u044f \u00ab\u0441\u043a\u0432\u043e\u0437\u043d\u044b\u0435\u00bb \u0432\u0435\u0442\u0432\u0438 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 \u0434\u043b\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0438\u0445  \u00ab\u043f\u043e\u0442\u043e\u043c\u043a\u0430\u043c\u0438\u00bb. \u0412 CTE \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043e\u043a\u043d\u043e <code>WindowByParents<\/code> \u0441 \u043f\u0430\u0440\u0442\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043f\u043e \u043f\u043e\u043b\u044e <code>Levels.parents<\/code>, \u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>LAST_VALUE(id) OVER WindowByParents<\/code> \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0441\u043e\u0431\u044b\u0439 \u0432\u0438\u0434 \u00ab\u0432\u0435\u0442\u0432\u0438\u00bb \u0434\u043b\u044f \u00ab\u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0445\u00bb \u0443\u0437\u043b\u043e\u0432 \u0443\u0440\u043e\u0432\u043d\u044f. \u0414\u043b\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043a\u043e\u0440\u043d\u0435\u0432\u044b\u0445\/\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0445 \u0443\u0437\u043b\u043e\u0432 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043b\u0435\u0432\u043e\u0435 \u0432\u043d\u0435\u0448\u043d\u0435\u0435 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 <code>Levels<\/code> \u0438 <code>RootNodes<\/code><\/p>\n<\/li>\n<li>\n<p><strong>Tree<\/strong>: \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e\u0435 CTE, \u0441\u043e\u0435\u0434\u0438\u043d\u044f\u044e\u0449\u0435\u0435 \u043d\u0430 \u043f\u0435\u0440\u0432\u043e\u043c \u0443\u0440\u043e\u0432\u043d\u0435 <code>Mapping<\/code> \u0438 <code>RootNodes<\/code> (\u0447\u0435\u0440\u0435\u0437 \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0435\u0435 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435), \u0438 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u0441\u043e\u0431\u0438\u0440\u0430\u044e\u0449\u0435\u0435 \u0432\u0441\u0435 \u0441\u043a\u0432\u043e\u0437\u043d\u044b\u0435 \u00ab\u0432\u0435\u0442\u043a\u0438\u00bb \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u0435\u0439 \u0443\u0437\u043b\u0430 \u0432 \u043f\u043e\u043b\u0435 <code>all_through<\/code><\/p>\n<\/li>\n<li>\n<p><strong>FineTree<\/strong>: \u0431\u0435\u0437 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439<\/p>\n<\/li>\n<\/ol>\n<h4>\u0418\u0437\u043c\u0435\u043d\u0435\u043d\u043d\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 (\u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0432\u0441\u0435 CTE)<\/h4>\n<details class=\"spoiler\">\n<summary>Mapping \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0444\u0430\u0439\u043b\u043e\u0432\u043e\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0438\u0437 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0438<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"sql\">with recursive  Mapping as ( select  id as node_id,  parent_directory_id as parent_node_id, name as node_name from Files ),  RootNodes as ( select node_id as root_node_id from Mapping where -- Exactly one line below should be uncommented parent_node_id is null-- Uncomment to build from root(s) -- node_id in (1)-- Uncomment to add node_id(s) into the brackets ),  Levels as ( select node_id, parent_node_id, node_name, cast(parent_node_id as char(2000)) as parents, cast(node_name as char(2000)) as full_path, 0 as node_level from Mapping inner join RootNodes on node_id = root_node_id  union  select  Mapping.node_id,  Mapping.parent_node_id, Mapping.node_name, concat(coalesce(concat(prev.parents, '-'), ''), cast(Mapping.parent_node_id as char)), concat_ws(' ', prev.full_path, Mapping.node_name), prev.node_level + 1 from  Levels as prev inner join Mapping on Mapping.parent_node_id = prev.node_id ),  Branches as ( select node_id, parent_node_id, node_name, parents, full_path, node_level, case when root_node_id is null then case when node_id = last_value(node_id) over WindowByParents then '\u2514\u2500\u2500 ' else '\u251c\u2500\u2500 ' end else '' end as node_branch, case when root_node_id is null then case when node_id = last_value(node_id) over WindowByParents then '    ' else '\u2502   ' end else '' end as branch_through from Levels left join RootNodes on node_id = root_node_id window WindowByParents as ( partition by parents order by node_name rows between current row and unbounded following ) order by full_path ),  Tree as ( select node_id, parent_node_id, node_name, parents, full_path, node_level, node_branch, cast(branch_through as char(2000)) as all_through from Branches inner join RootNodes on node_id = root_node_id  union  select  Branches.node_id, Branches.parent_node_id, Branches.node_name, Branches.parents, Branches.full_path, Branches.node_level, Branches.node_branch, concat(prev.all_through, Branches.branch_through) from  Tree as prev inner join Branches on Branches.parent_node_id = prev.node_id ),  FineTree as ( select tr.node_id, tr.parent_node_id, tr.node_name, tr.parents, tr.full_path, tr.node_level, concat(coalesce(parent.all_through, ''), tr.node_branch, tr.node_name) as fine_tree from Tree as tr left join Tree as parent on parent.node_id = tr.parent_node_id order by tr.full_path )  select fine_tree, node_id from FineTree ;<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u041f\u0440\u0438\u0448\u043b\u043e\u0441\u044c \u043e\u0442\u0441\u0442\u0443\u043f\u0438\u0442\u044c \u043e\u0442 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0430 \u0438\u0437\u043e\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0441\u0442\u0438 CTE \u043f\u043e &#171;\u0443\u0440\u043e\u0432\u043d\u044f\u043c&#187; &#8212; <code>RootNodes<\/code> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0442\u0440\u0438\u0436\u0434\u044b \u0432 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f\u0445 \u0432 \u0440\u0430\u0437\u043d\u044b\u0445 CTE. \u041d\u043e, \u0442\u0430\u043a \u043a\u0430\u043a \u043e\u043d \u043d\u0435 \u0438\u0437\u0432\u043b\u0435\u043a\u0430\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0438 \u0434\u043b\u044f \u0432\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438, \u0430 \u0441\u043b\u0443\u0436\u0438\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0430, \u0431\u0443\u0434\u0443 \u0441\u0447\u0438\u0442\u0430\u0442\u044c, \u0447\u0442\u043e \u044d\u0442\u043e \u043d\u0435 \u0441\u0447\u0438\u0442\u0430\u0435\u0442\u0441\u044f )<\/p>\n<details class=\"spoiler\">\n<summary>\u0421 \u0431\u0430\u0437\u043e\u0432\u044b\u043c \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u043c RootNodes&#8230;<\/summary>\n<div class=\"spoiler__content\">\n<figure class=\"float full-width\">\n<div><figcaption>SQLite<\/figcaption><\/div>\n<\/figure>\n<p>&#8230; \u0437\u0430\u043f\u0440\u043e\u0441 \u043e\u0442\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u0442\u0430\u043a \u0436\u0435, \u043a\u0430\u043a \u0432 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0447\u0430\u0441\u0442\u0438 \u0441\u0442\u0430\u0442\u044c\u0438, \u0438 \u0432 MySQL 8.2, \u0438 \u0432 SQLite 3.44.1<\/p>\n<\/div>\n<\/details>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u043e\u0432\u0435\u0440\u044e \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0447\u0430\u0441\u0442\u0438 \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0438, \u043f\u043e\u0434\u0441\u0442\u0430\u0432\u0438\u0432 \u0418\u0414 3 \u0438 11 \u0432 \u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u043d\u044b\u0439 <code>RootNodes<\/code>:<\/p>\n<pre><code class=\"sql\">RootNodes as ( select node_id as root_node_id from Mapping where -- Keep uncommented exactly one line below  -- parent_node_id is null-- Uncomment to build from root(s) node_id in (3, 11)-- Uncomment to add node_id(s) into the brackets ),<\/code><\/pre>\n<figure class=\"float\">\n<div><figcaption>MySQL<\/figcaption><\/div>\n<\/figure>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0441 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u044f\u043c: \u0432\u044b\u0432\u0435\u0434\u0435\u043d\u0430 \u0447\u0430\u0441\u0442\u044c \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0438, \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0443\u0437\u043b\u043e\u0432 \u0441 \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u0432 <code>RootNodes<\/code> \u0418\u0414<\/p>\n<h2>\u0415\u0449\u0451 \u043e\u0434\u043d\u0430 \u0444\u0430\u0439\u043b\u043e\u0432\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430<\/h2>\n<p>\u0412\u043e\u0437\u044c\u043c\u0443 \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0438\u0437 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u043d\u043e\u0439 \u0432 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 <a href=\"https:\/\/stepik.org\/lesson\/1195940\/step\/2?unit=1230007\" rel=\"noopener noreferrer nofollow\">\u0442\u043e\u0439 \u0436\u0435 \u0441\u0430\u043c\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438<\/a> \u0438\u0437 <a href=\"https:\/\/stepik.org\/course\/113918\/info\" rel=\"noopener noreferrer nofollow\">\u0442\u043e\u0433\u043e \u0441\u0430\u043c\u043e\u0433\u043e \u043a\u0443\u0440\u0441\u0430 \u043f\u043e SQL<\/a> \u043d\u0430 stepik.org &#8212; \u043d\u0430\u0434\u0435\u044e\u0441\u044c \u043d\u0430 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435 \u0441\u043d\u0438\u0441\u0445\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0430\u0432\u0442\u043e\u0440\u043e\u0432, \u043d\u043e \u043f\u043e\u0441\u0442\u0430\u0440\u0430\u044e\u0441\u044c \u0431\u043e\u043b\u044c\u0448\u0435 \u0435\u0439 \u043d\u0435 \u0437\u043b\u043e\u0443\u043f\u043e\u0442\u0440\u0435\u0431\u043b\u044f\u0442\u044c, \u0434\u043b\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0447\u0430\u0441\u0442\u0438 \u0441\u043e\u0437\u0434\u0430\u043c \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u044e \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e ))<\/p>\n<details class=\"spoiler\">\n<summary>\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0442\u0430\u0431\u043b\u0438\u0446\u044b Files \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0430, \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u0442\u0440\u043e\u043a \u0432\u044b\u0440\u043e\u0441\u043b\u043e \u043f\u0440\u0438 \u0442\u043e\u0439 \u0436\u0435 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0441\u0442\u0438<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"sql\">DROP TABLE IF EXISTS Files; CREATE TABLE Files (     id                  INT,     name                VARCHAR(40),     parent_directory_id INT );  INSERT INTO Files (id, name, parent_directory_id) VALUES  (1, 'desktop', null),         (2, 'another_folder', 1),         (3, 'FOLDER', 2),         (4, 'code.jpeg', 3),         (5, 'player10.json', 3),         (6, 'player11.json', 3),         (7, 'player5.json', 3),         (8, 'player6.json', 3),         (9, 'stepik.png', 3),         (10, 'FOLDER_HERE', 2),         (11, 'Crying All the Time.mp3', 10),         (12, 'player1.json', 10),         (13, 'player14.json', 10),         (14, 'player39.json', 2),         (15, 'player45.json', 2),         (16, 'player46.json', 2),         (17, 'python.pdf', 2),         (18, 'shopping_list.txt', 2),         (19, 'folder', 1),         (20, 'player19.json', 19),         (21, 'player20.json', 19),         (22, 'player21.json', 19),         (23, 'player42.json', 19),         (24, 'player47.json', 19),         (25, 'player48.json', 19),         (26, 'player49.json', 19);<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u0418\u0435\u0440\u0430\u0440\u0445\u0438\u044f \u0432 \u0431\u0430\u0437\u043e\u0432\u043e\u043c \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0435 <code>RootNodes<\/code> \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u044f\u043c (MySQL):<\/p>\n<pre><code class=\"bash\">fine_tree                          |node_id| -----------------------------------+-------+ desktop                            |      1| \u251c\u2500\u2500 another_folder                 |      2| \u2502   \u251c\u2500\u2500 FOLDER                     |      3| \u2502   \u2502   \u251c\u2500\u2500 code.jpeg              |      4| \u2502   \u2502   \u251c\u2500\u2500 player10.json          |      5| \u2502   \u2502   \u251c\u2500\u2500 player11.json          |      6| \u2502   \u2502   \u251c\u2500\u2500 player5.json           |      7| \u2502   \u2502   \u251c\u2500\u2500 player6.json           |      8| \u2502   \u2502   \u2514\u2500\u2500 stepik.png             |      9| \u2502   \u251c\u2500\u2500 FOLDER_HERE                |     10| \u2502   \u2502   \u251c\u2500\u2500 Crying All the Time.mp3|     11| \u2502   \u2502   \u251c\u2500\u2500 player1.json           |     12| \u2502   \u2502   \u2514\u2500\u2500 player14.json          |     13| \u2502   \u251c\u2500\u2500 player39.json              |     14| \u2502   \u251c\u2500\u2500 player45.json              |     15| \u2502   \u251c\u2500\u2500 player46.json              |     16| \u2502   \u251c\u2500\u2500 python.pdf                 |     17| \u2502   \u2514\u2500\u2500 shopping_list.txt          |     18| \u2514\u2500\u2500 folder                         |     19|     \u251c\u2500\u2500 player19.json              |     20|     \u251c\u2500\u2500 player20.json              |     21|     \u251c\u2500\u2500 player21.json              |     22|     \u251c\u2500\u2500 player42.json              |     23|     \u251c\u2500\u2500 player47.json              |     24|     \u251c\u2500\u2500 player48.json              |     25|     \u2514\u2500\u2500 player49.json              |     26|<\/code><\/pre>\n<p>\u0422\u0430\u043a \u0436\u0435 \u0447\u0430\u0441\u0442\u044c \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0438:<\/p>\n<pre><code class=\"sql\">RootNodes as ( select node_id as root_node_id from Mapping where -- Keep uncommented exactly one line below  -- parent_node_id is null-- Uncomment to build from root(s) node_id in (3, 10, 17)-- Uncomment to add node_id(s) into the brackets ),<\/code><\/pre>\n<figure class=\"float\">\n<div><figcaption>SQLite<\/figcaption><\/div>\n<\/figure>\n<p><a class=\"anchor\" name=\"slash_breaks_hierarchy\" id=\"slash_breaks_hierarchy\"><\/a><\/p>\n<p>\u0415\u0441\u0442\u044c \u043d\u044e\u0430\u043d\u0441 &#8212; \u0432 \u044d\u0442\u043e\u0439 \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0438 \u043d\u0430 \u043e\u0434\u043d\u043e\u043c \u0443\u0440\u043e\u0432\u043d\u0435 \u043d\u0430\u0445\u043e\u0434\u044f\u0442\u0441\u044f &#171;\u043f\u0430\u043f\u043a\u0438&#187; \u0441 \u043f\u043e\u0445\u043e\u0436\u0438\u043c\u0438 \u0438\u043c\u0435\u043d\u0430\u043c\u0438 <code>\"FOLDER\"<\/code> \u0438 <code>\"FOLDER_HERE\"<\/code>. \u0415\u0441\u043b\u0438 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u0445 \u0438\u043c\u0435\u043d\u0430 \u0432 \u0441\u043f\u0438\u0441\u043a\u0435 \u043f\u043e\u0442\u043e\u043c\u043a\u043e\u0432 \u0443\u0437\u043b\u0430 <code>\"another_folder\"<\/code>, \u043e\u043d\u0438 \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0430\u0442\u0441\u044f \u0432 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435, \u043d\u043e \u0435\u0441\u043b\u0438 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u0441\u0435\u0445 \u0443\u0437\u043b\u043e\u0432 \u043f\u043e \u043f\u043e\u043b\u043d\u044b\u043c \u043f\u0443\u0442\u044f\u043c, \u043a\u0430\u043a \u0432 CTE <code>FineTree<\/code> \u0434\u043b\u044f \u0444\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f, \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u0432\u0438\u0441\u0435\u0442\u044c \u043e\u0442 \u0441\u0438\u043c\u0432\u043e\u043b\u0430-\u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u0435\u043b\u044f, \u0447\u0435\u0440\u0435\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0431\u0440\u0430\u043d\u0430 \u0441\u0442\u0440\u043e\u043a\u0430 <code>full_path<\/code>. \u0415\u0441\u043b\u0438 \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0431\u0435\u043b <code>\" \"<\/code> \u043a\u043e\u0441\u043e\u0439 \u0447\u0435\u0440\u0442\u043e\u0439 <code>\"\/\"<\/code>, \u043f\u0440\u0438\u0432\u044b\u0447\u043d\u043e\u0439 \u0434\u043b\u044f \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u043f\u0443\u0442\u0435\u0439 \u0432 \u0444\u0430\u0439\u043b\u043e\u0432\u043e\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0435, \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0431\u0443\u0434\u0435\u0442 \u043d\u0435\u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u044b\u043c:<\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u0423\u0437\u0435\u043b FOLDER &#171;\u043f\u043e\u0442\u0435\u0440\u044f\u043b&#187; \u0441\u0432\u043e\u0438\u0445 \u043f\u043e\u0442\u043e\u043c\u043a\u043e\u0432, \u0442.\u043a. FOLDER_HERE\/ \u0438\u0434\u0435\u0442 \u0440\u0430\u043d\u044c\u0448\u0435 FOLDER\/<\/figcaption><\/div>\n<\/figure>\n<h2>\u0414\u0440\u0443\u0433\u0430\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u0430 &#8212; \u043a\u0430\u0442\u0435\u0433\u043e\u0440\u0438\u0438 \u0442\u043e\u0432\u0430\u0440\u043e\u0432<\/h2>\n<p>\u041f\u0440\u0438\u0432\u0435\u0434\u0443 \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0441\u043e \u0441\u0445\u043e\u0436\u0435\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b, \u043d\u043e \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u043d\u044b\u043c\u0438 \u0438\u043c\u0435\u043d\u0430\u043c\u0438,<\/p>\n<details class=\"spoiler\">\n<summary>\u043a\u043e\u043c\u0443 \u044d\u0442\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"sql\">DROP TABLE IF EXISTS Categories; CREATE TABLE Categories (     category_id         INT,     parent_category_id INT,     name                VARCHAR(40) );  INSERT INTO Categories (category_id, parent_category_id, name) VALUES (1, NULL, '\u0422\u043e\u0432\u0430\u0440\u044b \u0434\u043b\u044f \u0434\u043e\u043c\u0430'), (2, NULL, '\u0426\u0438\u0444\u0440\u043e\u0432\u0430\u044f \u0442\u0435\u0445\u043d\u0438\u043a\u0430'), (3, 1, '\u0411\u044b\u0442\u043e\u0432\u0430\u044f \u0442\u0435\u0445\u043d\u0438\u043a\u0430'), (4, 2, '\u041d\u043e\u0443\u0442\u0431\u0443\u043a\u0438 \u0438 \u0430\u043a\u0441\u0435\u0441\u0441\u0443\u0430\u0440\u044b'), (5, 2, '\u0424\u043e\u0442\u043e\u0430\u043f\u043f\u0430\u0440\u0430\u0442\u044b'), (6, 2, '\u0418\u0433\u0440\u043e\u0432\u044b\u0435 \u043a\u043e\u043d\u0441\u043e\u043b\u0438'), (7, 2, '\u0410\u0443\u0434\u0438\u043e\u0442\u0435\u0445\u043d\u0438\u043a\u0430'), (8, 2, '\u0421\u043e\u0442\u043e\u0432\u044b\u0435 \u0442\u0435\u043b\u0435\u0444\u043e\u043d\u044b'), (9, 4, '\u041d\u043e\u0443\u0442\u0431\u0443\u043a\u0438'), (10, 4, '\u0420\u044e\u043a\u0437\u0430\u043a\u0438');<\/code><\/pre>\n<p>\u0414\u043b\u044f \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043d\u0430 \u043d\u043e\u0432\u044b\u0439 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a \u043d\u0443\u0436\u043d\u043e \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c <code>Mapping<\/code>:<\/p>\n<pre><code class=\"sql\">with recursive  Mapping as ( select  category_idas node_id,  parent_category_idas parent_node_id, name as node_name from Categories ),<\/code><\/pre>\n<figure class=\"float\">\n<div><figcaption>SQLite<\/figcaption><\/div>\n<\/figure>\n<p>\u0417\u0430\u043f\u0440\u043e\u0441 \u0432 \u0431\u0430\u0437\u043e\u0432\u043e\u043c \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0435 \u043e\u0442\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442<\/p>\n<\/div>\n<\/details>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-374869","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/374869","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=374869"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/374869\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=374869"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=374869"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=374869"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}