{"id":6484,"date":"2026-06-21T13:53:05","date_gmt":"2026-06-21T04:53:05","guid":{"rendered":"https:\/\/route-zero.com\/recruit\/?post_type=route&#038;p=6484"},"modified":"2026-10-21T13:53:16","modified_gmt":"2026-10-21T04:53:16","slug":"sql-with-recursive-hierarchy-cycle-safety","status":"publish","type":"route","link":"https:\/\/route-zero.com\/recruit\/route\/6484\/","title":{"rendered":"SQL\u306eWITH RECURSIVE\u3067\u89aa\u5b50\u968e\u5c64\u3092\u53d6\u5f97\u3059\u308b\u65b9\u6cd5"},"content":{"rendered":"<p>\u89aa\u5b50\u95a2\u4fc2\u3092\u6301\u3064\u30ab\u30c6\u30b4\u30ea\u3084\u90e8\u7f72\u3092SQL\u3067\u968e\u5c64\u7684\u306b\u53d6\u5f97\u3059\u308b\u306b\u306f\u3001WITH RECURSIVE\u3092\u4f7f\u3044\u307e\u3059\u3002\u6700\u521d\u306e\u89aa\u3092\u53d6\u5f97\u3059\u308b\u300c\u8d77\u70b9SELECT\u300d\u3068\u3001\u305d\u306e\u5b50\u3092\u7e70\u308a\u8fd4\u3057\u63a2\u3059\u300c\u518d\u5e30SELECT\u300d\u3092UNION ALL\u3067\u3064\u306a\u3050\u306e\u304c\u57fa\u672c\u3067\u3059\u3002\u5faa\u74b0\u3057\u305f\u53c2\u7167\u30c7\u30fc\u30bf\u3067\u306f\u51e6\u7406\u304c\u7d42\u308f\u3089\u306a\u3044\u304a\u305d\u308c\u304c\u3042\u308b\u305f\u3081\u3001PostgreSQL 14\u4ee5\u964d\u306a\u3089CYCLE\u53e5\u306a\u3069\u3067\u5faa\u74b0\u3092\u691c\u51fa\u3057\u3001\u5fc5\u8981\u306b\u5fdc\u3058\u3066\u6700\u5927\u6df1\u3055\u3082\u5236\u9650\u3057\u307e\u3059\u3002\u3053\u3053\u3067\u306fPostgreSQL\u3067\u66f8\u304d\u65b9\u3092\u78ba\u8a8d\u3057\u307e\u3059\u3002<\/p>\n<h2>WITH RECURSIVE\u306f\u8d77\u70b9\u3068\u518d\u5e30\u51e6\u7406\u3092\u7d44\u307f\u5408\u308f\u305b\u308b<\/h2>\n<p>\u518d\u5e30CTE\u306f\u3001\u65e2\u306b\u53d6\u5f97\u3057\u305f\u884c\u306e\u5024\u3092\u4f7f\u3063\u3066\u6b21\u306e\u884c\u3092\u63a2\u3059SQL\u3067\u3059\u3002\u7d44\u7e54\u30c4\u30ea\u30fc\u3001\u30ab\u30c6\u30b4\u30ea\u968e\u5c64\u3001\u90e8\u54c1\u8868\u306a\u3069\u3001\u89aa\u5b50\u95a2\u4fc2\u304c\u7e70\u308a\u8fd4\u3055\u308c\u308b\u30c7\u30fc\u30bf\u306b\u5411\u3044\u3066\u3044\u307e\u3059\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th>\u90e8\u5206<\/th>\n<th>\u5f79\u5272<\/th>\n<th>\u4f8b<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u8d77\u70b9\uff08\u975e\u518d\u5e30\u9805\uff09<\/td>\n<td>\u63a2\u7d22\u958b\u59cb\u884c\u3092\u53d6\u5f97<\/td>\n<td>\u89aa\u304c\u306a\u3044\u6700\u4e0a\u4f4d\u30ab\u30c6\u30b4\u30ea\u3084\u6307\u5b9aid<\/td>\n<\/tr>\n<tr>\n<td>UNION ALL<\/td>\n<td>\u8d77\u70b9\u3068\u518d\u5e30\u7d50\u679c\u3092\u7d50\u5408<\/td>\n<td>\u968e\u5c64\u3092\u7e70\u308a\u8fd4\u3057\u5897\u3084\u3059<\/td>\n<\/tr>\n<tr>\n<td>\u518d\u5e30\u9805<\/td>\n<td>\u76f4\u524d\u306b\u5f97\u305f\u89aa\u306b\u7d10\u3065\u304f\u5b50\u3092\u53d6\u5f97<\/td>\n<td>c.parent_id = t.id<\/td>\n<\/tr>\n<tr>\n<td>\u7d42\u4e86\u6761\u4ef6<\/td>\n<td>\u6b21\u306b\u4e00\u81f4\u3059\u308b\u5b50\u304c\u306a\u304f\u306a\u308c\u3070\u7d42\u4e86<\/td>\n<td>\u5b50\u304c\u306a\u3044\u8449\u30ce\u30fc\u30c9\u306a\u3069<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<blockquote>\n<p><strong>\u7528\u8a9e\u89e3\u8aac\uff1aCTE\uff08\u5171\u901a\u30c6\u30fc\u30d6\u30eb\u5f0f\uff09<\/strong><br \/>WITH\u3067\u5b9a\u7fa9\u3057\u3066\u30af\u30a8\u30ea\u5185\u304b\u3089\u540d\u524d\u3067\u53c2\u7167\u3067\u304d\u308b\u4e00\u6642\u7684\u306a\u7d50\u679c\u96c6\u5408\u3002WITH RECURSIVE\u3067\u306f\u5b9a\u7fa9\u3057\u305fCTE\u3092\u518d\u5e30\u9805\u304b\u3089\u53c2\u7167\u3067\u304d\u307e\u3059\u3002<\/p>\n<\/blockquote>\n<p>\u4ee5\u4e0b\u306fPostgreSQL 18\u3092\u53c2\u7167\u3057\u305f\u8aac\u660e\u3067\u3059\u3002MySQL\u3084SQL Server\u3067\u306f\u4f7f\u7528\u3067\u304d\u308b\u518d\u5e30\u69cb\u6587\u3084\u5faa\u74b0\u691c\u51fa\u306e\u65b9\u6cd5\u304c\u7570\u306a\u308b\u305f\u3081\u3001\u540c\u3058SQL\u3092\u305d\u306e\u307e\u307e\u6d41\u7528\u3057\u306a\u3044\u3067\u304f\u3060\u3055\u3044\u3002<\/p>\n<hr>\n<h2>\u30b5\u30f3\u30d7\u30eb\u306e\u30ab\u30c6\u30b4\u30ea\u968e\u5c64\u3092\u7528\u610f\u3059\u308b<\/h2>\n<p>\u6b21\u306e\u4eee\u60f3\u30c7\u30fc\u30bf\u3092\u60f3\u5b9a\u3057\u307e\u3059\u3002id=1\u306e\u300c\u5168\u793e\u300d\u3092\u8d77\u70b9\u306b\u3001\u958b\u767a\u90e8\u3068\u55b6\u696d\u90e8\u3001\u305d\u306e\u4e0b\u306e\u5b50\u30ab\u30c6\u30b4\u30ea\u3092\u8fbf\u308a\u307e\u3059\u3002\u307e\u305a\u30c6\u30fc\u30d6\u30eb\u8a2d\u8a08\u3068\u30c7\u30fc\u30bf\u306e\u95a2\u4fc2\u3092\u78ba\u8a8d\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th>id<\/th>\n<th>parent_id<\/th>\n<th>name<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>NULL<\/td>\n<td>\u5168\u793e<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>1<\/td>\n<td>\u958b\u767a\u90e8<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>2<\/td>\n<td>Web\u30c1\u30fc\u30e0<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>2<\/td>\n<td>\u30a4\u30f3\u30d5\u30e9\u30c1\u30fc\u30e0<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>1<\/td>\n<td>\u55b6\u696d\u90e8<\/td>\n<\/tr>\n<tr>\n<td>6<\/td>\n<td>5<\/td>\n<td>\u6cd5\u4eba\u55b6\u696d<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<pre><code>CREATE TABLE categories (\r\n    id integer PRIMARY KEY,\r\n    parent_id integer REFERENCES categories(id),\r\n    name text NOT NULL\r\n);\r\n\r\nINSERT INTO categories (id, parent_id, name) VALUES\r\n(1, NULL, &#39;\u5168\u793e&#39;),\r\n(2, 1, &#39;\u958b\u767a\u90e8&#39;),\r\n(3, 2, &#39;Web\u30c1\u30fc\u30e0&#39;),\r\n(4, 2, &#39;\u30a4\u30f3\u30d5\u30e9\u30c1\u30fc\u30e0&#39;),\r\n(5, 1, &#39;\u55b6\u696d\u90e8&#39;),\r\n(6, 5, &#39;\u6cd5\u4eba\u55b6\u696d&#39;);<\/code><\/pre>\n<p>CREATE TABLE\u3068INSERT\u306f\u691c\u8a3c\u7528\u30c7\u30fc\u30bf\u306e\u4f5c\u6210\u4f8b\u3067\u3059\u3002\u5b9f\u969b\u306e\u696d\u52d9\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u540c\u540d\u306e\u8868\u304c\u3042\u308b\u5834\u5408\u3001\u305d\u306e\u307e\u307e\u5b9f\u884c\u3057\u306a\u3044\u3067\u304f\u3060\u3055\u3044\u3002\u8aad\u307f\u53d6\u308a\u5c02\u7528\u306eSELECT\u4f8b\u306f\u6b21\u306e\u7bc0\u306b\u793a\u3057\u307e\u3059\u3002<\/p>\n<hr>\n<h2>\u518d\u5e30CTE\u3067\u5b50\u5b6b\u3092\u3059\u3079\u3066\u53d6\u5f97\u3059\u308bSQL<\/h2>\n<pre><code>WITH RECURSIVE category_tree AS (\r\n  SELECT id, parent_id, name, 0 AS depth\r\n  FROM categories\r\n  WHERE id = 1\r\n\r\n  UNION ALL\r\n\r\n  SELECT c.id, c.parent_id, c.name, t.depth + 1\r\n  FROM categories AS c\r\n  JOIN category_tree AS t\r\n    ON c.parent_id = t.id\r\n)\r\nSELECT id, parent_id, name, depth\r\nFROM category_tree\r\nORDER BY depth, id;<\/code><\/pre>\n<p>\u6700\u521d\u306bid=1\u3092depth=0\u3068\u3057\u3066\u8fd4\u3057\u3001\u305d\u3053\u304b\u3089\u5b50\u306b\u4e00\u81f4\u3059\u308b\u884c\u3092\u53d6\u5f97\u3057\u307e\u3059\u3002\u6b21\u306e\u518d\u5e30\u3067\u3055\u3089\u306b\u5b6b\u3078\u9032\u307f\u3001\u8a72\u5f53\u3059\u308b\u5b50\u304c\u306a\u304f\u306a\u3063\u305f\u3068\u304d\u306b\u7d42\u4e86\u3057\u307e\u3059\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th>id<\/th>\n<th>name<\/th>\n<th>depth<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>\u5168\u793e<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>\u958b\u767a\u90e8<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>\u55b6\u696d\u90e8<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>Web\u30c1\u30fc\u30e0<\/td>\n<td>2<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>\u30a4\u30f3\u30d5\u30e9\u30c1\u30fc\u30e0<\/td>\n<td>2<\/td>\n<\/tr>\n<tr>\n<td>6<\/td>\n<td>\u6cd5\u4eba\u55b6\u696d<\/td>\n<td>2<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8868\u793a\u4f8b\u306f\u30b5\u30f3\u30d7\u30eb\u306e\u30c7\u30fc\u30bf\u95a2\u4fc2\u304b\u3089\u5c0e\u3044\u305f\u671f\u5f85\u7d50\u679c\u3067\u3059\u3002\u5b9f\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3067SQL\u3092\u52d5\u304b\u3057\u3066\u691c\u8a3c\u3057\u305f\u7d50\u679c\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002ORDER BY depth,id\u3092\u6307\u5b9a\u3057\u3066\u3044\u308b\u305f\u3081\u3001\u968e\u5c64\u306e\u6df1\u3055\u3068ID\u3067\u4e26\u3079\u3066\u3044\u307e\u3059\u3002<\/p>\n<hr>\n<h2>SQL\u304c\u7d42\u308f\u3089\u306a\u3044\u539f\u56e0\u3068CYCLE\u53e5\u306b\u3088\u308b\u5bfe\u7b56<\/h2>\n<p>\u89aa\u5b50\u30c7\u30fc\u30bf\u306b\u5faa\u74b0\u3057\u305f\u53c2\u7167\u304c\u3042\u308b\u3068\u3001\u518d\u5e30\u51e6\u7406\u304c\u540c\u3058\u30ce\u30fc\u30c9\u3092\u7e70\u308a\u8fd4\u3057\u3066\u3057\u307e\u3046\u3053\u3068\u304c\u3042\u308a\u307e\u3059\u3002\u7279\u306bUNION ALL\u306f\u91cd\u8907\u3092\u9664\u5916\u3057\u306a\u3044\u305f\u3081\u3001\u5faa\u74b0\u53c2\u7167\u3092\u305d\u306e\u307e\u307e\u8fbf\u308bSQL\u306b\u306f\u6ce8\u610f\u304c\u5fc5\u8981\u3067\u3059\u3002<\/p>\n<pre><code>WITH RECURSIVE category_tree AS (\r\n  SELECT id, parent_id, name, 0 AS depth\r\n  FROM categories\r\n  WHERE id = 1\r\n\r\n  UNION ALL\r\n\r\n  SELECT c.id, c.parent_id, c.name, t.depth + 1\r\n  FROM categories AS c\r\n  JOIN category_tree AS t\r\n    ON c.parent_id = t.id\r\n  WHERE t.depth &lt; 20\r\n)\r\nCYCLE id SET is_cycle USING path\r\nSELECT id, name, depth\r\nFROM category_tree\r\nWHERE NOT is_cycle\r\nORDER BY path;<\/code><\/pre>\n<p>PostgreSQL 14\u4ee5\u964d\u306eCYCLE\u53e5\u306f\u3001\u540c\u3058ID\u3092\u518d\u8a2a\u3057\u305f\u304b\u3092\u8ffd\u8de1\u3059\u308bis_cycle\u5217\u3068\u7d4c\u8def\u3092\u793a\u3059path\u5217\u3092\u81ea\u52d5\u751f\u6210\u3057\u307e\u3059\u3002\u5faa\u74b0\u304c\u691c\u51fa\u3055\u308c\u308b\u3068\u3001\u305d\u306e\u7d4c\u8def\u306e\u518d\u5e30\u3092\u305d\u3053\u3067\u6b62\u3081\u307e\u3059\u3002\u3053\u3053\u3067\u306f\u6df1\u3055\u306e\u4e0a\u9650\u308220\u6bb5\u3068\u3057\u3001\u51fa\u529b\u3067\u306f\u5faa\u74b0\u884c\u3092\u9664\u5916\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u6df1\u305520\u3068\u3044\u3046\u6570\u5024\u306f\u8aac\u660e\u7528\u306e\u5b89\u5168\u4e0a\u9650\u3067\u3059\u3002\u5b9f\u969b\u306e\u968e\u5c64\u304c21\u6bb5\u4ee5\u4e0a\u3042\u308b\u5834\u5408\u306f\u5fc5\u8981\u306a\u884c\u304c\u6b20\u3051\u308b\u306e\u3067\u3001\u696d\u52d9\u4e0a\u60f3\u5b9a\u3055\u308c\u308b\u6df1\u3055\u3092\u78ba\u8a8d\u3057\u3066\u8a2d\u5b9a\u3057\u3066\u304f\u3060\u3055\u3044\u3002CYCLE\u3092\u30b5\u30dd\u30fc\u30c8\u3057\u306a\u3044DB\u3067\u306f\u540c\u3058\u69cb\u6587\u3092\u4f7f\u7528\u3067\u304d\u307e\u305b\u3093\u3002<\/p>\n<h2>UNION\u3068UNION ALL\u3001LIMIT\u306e\u6ce8\u610f\u70b9<\/h2>\n<p>UNION\u306f\u540c\u4e00\u306e\u51fa\u529b\u884c\u3092\u91cd\u8907\u6392\u9664\u3057\u307e\u3059\u304c\u3001\u6df1\u3055\u3084\u7d4c\u8def\u304c\u6bce\u56de\u5909\u308f\u308b\u518d\u5e30\u3067\u306f\u3001\u5faa\u74b0\u304c\u3042\u3063\u3066\u3082\u884c\u5168\u4f53\u306f\u7570\u306a\u308b\u305f\u3081\u3001UNION\u306b\u7f6e\u304d\u63db\u3048\u308b\u3060\u3051\u3067\u5b89\u5168\u306b\u306a\u308b\u3068\u306f\u9650\u308a\u307e\u305b\u3093\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th>\u5de5\u592b<\/th>\n<th>\u5f97\u3089\u308c\u308b\u3053\u3068<\/th>\n<th>\u6ce8\u610f\u70b9<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>UNION ALL<\/td>\n<td>\u518d\u5e30\u7d50\u679c\u3092\u305d\u306e\u307e\u307e\u52a0\u3048\u308b<\/td>\n<td>\u5faa\u74b0\u306e\u91cd\u8907\u306b\u6ce8\u610f<\/td>\n<\/tr>\n<tr>\n<td>UNION<\/td>\n<td>\u540c\u4e00\u306e\u884c\u3092\u91cd\u8907\u9664\u5916<\/td>\n<td>depth\u304c\u5909\u308f\u308c\u3070\u5faa\u74b0\u9632\u6b62\u3067\u304d\u306a\u3044<\/td>\n<\/tr>\n<tr>\n<td>CYCLE<\/td>\n<td>\u8a2a\u554f\u6e08\u307fID\u3092\u691c\u51fa<\/td>\n<td>\u5bfe\u5fdcDB\u3068\u30d0\u30fc\u30b8\u30e7\u30f3\u304c\u5fc5\u8981<\/td>\n<\/tr>\n<tr>\n<td>depth\u4e0a\u9650<\/td>\n<td>\u518d\u5e30\u56de\u6570\u306b\u4e0a\u9650\u3092\u8a2d\u3051\u308b<\/td>\n<td>\u6df1\u3044\u6b63\u3057\u3044\u968e\u5c64\u3082\u9014\u4e2d\u3067\u5207\u308c\u308b<\/td>\n<\/tr>\n<tr>\n<td>\u5916\u5074SELECT\u306eLIMIT<\/td>\n<td>\u8fd4\u3059\u884c\u6570\u3092\u5236\u9650<\/td>\n<td>ORDER BY\u306a\u3069\u3067\u4e2d\u9593\u7d50\u679c\u5168\u4f53\u304c\u5fc5\u8981\u3068\u306a\u308b\u5834\u5408\u3042\u308a<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5916\u5074\u306bLIMIT 10\u3092\u4ed8\u3051\u305f\u304b\u3089\u7121\u9650\u518d\u5e30\u3092\u78ba\u5b9f\u306b\u9632\u3052\u308b\u3001\u3068\u306f\u8003\u3048\u306a\u3044\u3067\u304f\u3060\u3055\u3044\u3002\u4e26\u3073\u66ff\u3048\u306a\u3069\u306e\u51e6\u7406\u306b\u3088\u3063\u3066\u306f\u3001\u9014\u4e2d\u7d50\u679c\u304c\u5e83\u304f\u8a55\u4fa1\u3055\u308c\u308b\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002CYCLE\u3084\u63a2\u7d22\u6761\u4ef6\u3067\u6839\u672c\u7684\u306b\u6b62\u3081\u308b\u65b9\u91dd\u3092\u512a\u5148\u3057\u307e\u3059\u3002<\/p>\n<h2>\u6df1\u3055\u512a\u5148\u30fb\u5e45\u512a\u5148\u3068\u8868\u793a\u9806\u3092\u8003\u3048\u308b<\/h2>\n<p>\u968e\u5c64\u306e\u53d6\u5f97\u9806\u3068\u753b\u9762\u3067\u898b\u305b\u308b\u4e26\u3073\u306f\u5225\u306e\u554f\u984c\u3067\u3059\u3002PostgreSQL\u306e\u518d\u5e30\u8a55\u4fa1\u9806\u306b\u4f9d\u5b58\u3057\u3066\u9806\u756a\u3092\u6c7a\u3081\u308b\u306e\u3067\u306f\u306a\u304f\u3001\u5916\u5074\u3067ORDER BY\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002<\/p>\n<p>CYCLE\u53e5\u3067\u4f5c\u3089\u308c\u308bpath\u3067\u30bd\u30fc\u30c8\u3059\u308c\u3070\u3001\u7d4c\u8def\u306b\u6cbf\u3063\u305f\u4e26\u3073\u306b\u3067\u304d\u307e\u3059\u3002\u6df1\u3055\u3054\u3068\u306b\u307e\u3068\u3081\u305f\u3044\u306a\u3089depth\u3068id\u3067\u4e26\u3079\u308b\u65b9\u6cd5\u304c\u5206\u304b\u308a\u3084\u3059\u3044\u3067\u3057\u3087\u3046\u3002PostgreSQL 14\u4ee5\u964d\u306b\u306fSEARCH DEPTH FIRST \/ BREADTH FIRST\u3082\u3042\u308a\u3001\u660e\u793a\u7684\u306a\u9806\u5e8f\u5217\u3092\u4f5c\u308c\u307e\u3059\u3002<\/p>\n<pre><code>-- CYCLE\u3067\u751f\u6210\u3055\u308c\u305fpath\u3092\u5229\u7528\u3059\u308b\u4f8b\r\nSELECT id, name, depth\r\nFROM category_tree\r\nWHERE NOT is_cycle\r\nORDER BY path;<\/code><\/pre>\n<p>\u3053\u306e\u77ed\u3044SQL\u306f\u76f4\u524d\u306eCYCLE\u4ed8\u304dCTE\u306e\u6700\u5f8c\u306eSELECT\u90e8\u5206\u3060\u3051\u3092\u629c\u304d\u51fa\u3057\u305f\u3082\u306e\u3067\u3059\u3002\u5358\u72ec\u3067\u5b9f\u884c\u3057\u3066\u3082category_tree\u306f\u5b58\u5728\u3057\u307e\u305b\u3093\u3002<\/p>\n<h2>\u5bfe\u8c61\u304c\u6b20\u3051\u308b\u30fb\u91cd\u8907\u3059\u308b\u5834\u5408\u306e\u78ba\u8a8d\u70b9<\/h2>\n<ul>\n<li>\u8d77\u70b9\u306eWHERE id = 1\u304c\u5b9f\u969b\u306b\u5b58\u5728\u3059\u308b\u304b\u3002<\/li>\n<li>c.parent_id = t.id\u306e\u5217\u3084\u578b\u304c\u6b63\u3057\u3044\u304b\u3002<\/li>\n<li>\u89aaid\u306b\u4e00\u81f4\u3059\u308b\u5b50\u306e\u30ec\u30b3\u30fc\u30c9\u304c\u3042\u308b\u304b\u3002<\/li>\n<li>\u30ab\u30c6\u30b4\u30ea\u304c\u8907\u6570\u306e\u89aa\u3092\u6301\u3064\u30b0\u30e9\u30d5\u69cb\u9020\u306a\u3089\u3001\u7d4c\u8def\u3054\u3068\u306b\u540c\u3058\u30ce\u30fc\u30c9\u304c\u73fe\u308c\u308b\u5834\u5408\u304c\u306a\u3044\u304b\u3002<\/li>\n<li>depth\u4e0a\u9650\u3067\u5fc5\u8981\u306a\u968e\u5c64\u3092\u9014\u4e2d\u3067\u6253\u3061\u5207\u3063\u3066\u3044\u306a\u3044\u304b\u3002<\/li>\n<li>\u518d\u5e30\u51e6\u7406\u306e\u6027\u80fd\u304c\u554f\u984c\u306a\u3089\u89aa\u53c2\u7167\u5217\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u691c\u8a0e\u3057\u3001\u5b9f\u884c\u8a08\u753b\u3092\u78ba\u8a8d\u3059\u308b\u3002<\/li>\n<\/ul>\n<p>\u5927\u91cf\u306e\u30ec\u30b3\u30fc\u30c9\u3067\u5b9f\u884c\u3059\u308b\u5834\u5408\u306f\u3001\u53d6\u5f97\u3059\u308b\u8d77\u70b9\u3068\u6df1\u3055\u3092\u9650\u5b9a\u3057\u305f\u691c\u8a3c\u30c7\u30fc\u30bf\u304b\u3089\u59cb\u3081\u307e\u3057\u3087\u3046\u3002\u30c7\u30fc\u30bf\u306e\u5faa\u74b0\u3092\u691c\u51fa\u3057\u305f\u5834\u5408\u306f\u3001SQL\u5074\u306e\u56de\u907f\u306b\u52a0\u3048\u3066\u53c2\u7167\u30c7\u30fc\u30bf\u306e\u6574\u5408\u6027\u3082\u78ba\u8a8d\u3059\u308b\u3053\u3068\u304c\u91cd\u8981\u3067\u3059\u3002<\/p>\n<h2>\u95a2\u9023\u3059\u308bSQL\u8a18\u4e8b<\/h2>\n<p>\u901a\u5e38\u306eCTE\u3084\u30b5\u30d6\u30af\u30a8\u30ea\u3068\u306e\u9055\u3044\u3092\u6574\u7406\u3057\u305f\u3044\u5834\u5408\u306f\u3001\u65e2\u5b58\u306eSQL\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u8a18\u4e8b\u304c\u5f79\u7acb\u3061\u307e\u3059\u3002\u307e\u305f\u518d\u5e30\u9805\u306eJOIN\u304c\u5206\u304b\u3089\u306a\u3044\u5834\u5408\u306f\u7d50\u5408\u306e\u57fa\u672c\u3092\u78ba\u8a8d\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<p>\uff08CTE\u306e\u8003\u3048\u65b9\u3068\u901a\u5e38\u306eSQL\u306b\u3064\u3044\u3066\u306f\u300e<a href=\"https:\/\/route-zero.com\/recruit\/route\/1594\/\">SQL\u30b5\u30d6\u30af\u30a8\u30ea\u306f\u9045\u3044\uff1fJOIN\u30fbCTE\u3068\u306e\u9055\u3044\u3068\u9ad8\u901f\u5316\u306e\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9<\/a>\u300f\u3092\u3054\u53c2\u7167\u304f\u3060\u3055\u3044\uff09<\/p>\n<p>\uff08\u89aa\u5b50\u30c6\u30fc\u30d6\u30eb\u3092\u7d50\u5408\u3059\u308b\u57fa\u672c\u306b\u3064\u3044\u3066\u306f\u300e<a href=\"https:\/\/route-zero.com\/recruit\/route\/1119\/\">SQL JOIN\u306e\u57fa\u672c\u304b\u3089\u5b9f\u8df5\u307e\u3067\uff5cINNER\u30fbLEFT\u30fbRIGHT\u30fbOUTER JOIN\u306e\u9055\u3044\u3092\u308f\u304b\u308a\u3084\u3059\u304f\u89e3\u8aac<\/a>\u300f\u3092\u3054\u53c2\u7167\u304f\u3060\u3055\u3044\uff09<\/p>\n<h2>\u3088\u304f\u3042\u308b\u8cea\u554f\u3068\u307e\u3068\u3081<\/h2>\n<h3>WITH RECURSIVE\u306f\u5168DB\u3067\u540c\u3058\u3088\u3046\u306b\u52d5\u304d\u307e\u3059\u304b\uff1f<\/h3>\n<p>\u3044\u3044\u3048\u3002\u57fa\u672c\u306e\u8003\u3048\u65b9\u306f\u5171\u901a\u3067\u3059\u304c\u3001\u518d\u5e30\u306e\u4e0a\u9650\u30fbCYCLE\u30fbSEARCH\u306a\u3069\u306f\u88fd\u54c1\u3068\u30d0\u30fc\u30b8\u30e7\u30f3\u3067\u7570\u306a\u308a\u307e\u3059\u3002\u672c\u6587\u306fPostgreSQL\u3092\u4f8b\u306b\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<h3>CYCLE\u304c\u3042\u308c\u3070depth\u4e0a\u9650\u306f\u4e0d\u8981\uff1f<\/h3>\n<p>\u5faa\u74b0\u3092\u691c\u51fa\u3059\u308b\u76ee\u7684\u3067\u306fCYCLE\u304c\u5f79\u7acb\u3061\u307e\u3059\u304c\u3001\u60f3\u5b9a\u5916\u306b\u975e\u5e38\u306b\u6df1\u3044\u6b63\u3057\u3044\u968e\u5c64\u3092\u5236\u9650\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u5834\u5408\u306f\u696d\u52d9\u8981\u4ef6\u306b\u6cbf\u3063\u305f\u4e0a\u9650\u3082\u691c\u8a0e\u3057\u307e\u3059\u3002<\/p>\n<h3>UNION\u306b\u5909\u66f4\u3059\u308b\u3060\u3051\u3067\u7121\u9650\u518d\u5e30\u3092\u9632\u3052\u307e\u3059\u304b\uff1f<\/h3>\n<p>\u51fa\u529b\u884c\u5168\u4f53\u304c\u540c\u4e00\u3067\u3042\u308b\u5834\u5408\u306f\u91cd\u8907\u6392\u9664\u304c\u50cd\u304d\u307e\u3059\u3002\u3057\u304b\u3057depth\u306e\u3088\u3046\u306b\u5024\u304c\u5909\u5316\u3059\u308b\u5217\u304c\u3042\u308b\u3068\u6b62\u307e\u3089\u306a\u3044\u305f\u3081\u3001\u78ba\u5b9f\u306a\u5bfe\u7b56\u3068\u306f\u9650\u308a\u307e\u305b\u3093\u3002<\/p>\n<p>\u518d\u5e30CTE\u306f\u3001\u8d77\u70b9\u3068\u5b50\u3092\u63a2\u3059\u518d\u5e30SELECT\u3092\u3064\u306a\u3044\u3067\u968e\u5c64\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u65b9\u6cd5\u3067\u3059\u3002\u52d5\u4f5c\u78ba\u8a8d\u306f\u5c11\u6570\u306e\u30b5\u30f3\u30d7\u30eb\u304b\u3089\u59cb\u3081\u3001\u5faa\u74b0\u691c\u51fa\u3068\u6df1\u3055\u5236\u9650\u3092\u8a2d\u3051\u3001\u8868\u793a\u9806\u3092\u660e\u793a\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<p>\u89aa\u5b50\u30c6\u30fc\u30d6\u30eb\u3092\u4f7f\u3063\u305f\u5c0f\u3055\u306a\u691c\u8a3cDB\u3067\u3001\u8d77\u70b9\u3068depth\u306e\u5909\u5316\u3001CYCLE\u306b\u3088\u308b\u7d42\u4e86\u3092\u78ba\u304b\u3081\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n","protected":false},"featured_media":6489,"template":"","meta":{"ssp_meta_title":"SQL WITH RECURSIVE\u3067\u968e\u5c64\u3092\u53d6\u5f97\uff5cCYCLE\u3068\u7121\u9650\u518d\u5e30\u5bfe\u7b56","ssp_meta_description":"WITH RECURSIVE\u3067\u89aa\u5b50\u968e\u5c64\u3092\u53d6\u5f97\u3059\u308bSQL\u4f8b\u3002PostgreSQL\u306e\u8d77\u70b9\u30fb\u518d\u5e30\u9805\u30fbdepth\u30fbCYCLE\u53e5\u3092\u4f7f\u3044\u3001\u7121\u9650\u518d\u5e30\u3068\u91cd\u8907\u30c7\u30fc\u30bf\u306e\u5bfe\u7b56\u307e\u3067\u89e3\u8aac\u3057\u307e\u3059\u3002","ssp_meta_keyword":"SQL WITH RECURSIVE \u968e\u5c64\u30c7\u30fc\u30bf,SQL \u518d\u5e30CTE \u89aa\u5b50,PostgreSQL WITH RECURSIVE,PostgreSQL CYCLE,SQL \u7121\u9650\u518d\u5e30 \u9632\u6b62","ssp_meta_canonical":""},"route_category":[9],"_links":{"self":[{"href":"https:\/\/route-zero.com\/recruit\/wp-json\/wp\/v2\/route\/6484"}],"collection":[{"href":"https:\/\/route-zero.com\/recruit\/wp-json\/wp\/v2\/route"}],"about":[{"href":"https:\/\/route-zero.com\/recruit\/wp-json\/wp\/v2\/types\/route"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/route-zero.com\/recruit\/wp-json\/wp\/v2\/media\/6489"}],"wp:attachment":[{"href":"https:\/\/route-zero.com\/recruit\/wp-json\/wp\/v2\/media?parent=6484"}],"wp:term":[{"taxonomy":"route_category","embeddable":true,"href":"https:\/\/route-zero.com\/recruit\/wp-json\/wp\/v2\/route_category?post=6484"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}