{"id":16701,"date":"2024-12-28T18:57:09","date_gmt":"2024-12-28T10:57:09","guid":{"rendered":"https:\/\/www.orczhou.com\/?p=16701"},"modified":"2024-12-27T18:59:10","modified_gmt":"2024-12-27T10:59:10","slug":"cte-in-mysql-8-and-more-details","status":"publish","type":"post","link":"https:\/\/www.orczhou.com\/index.php\/2024\/12\/cte-in-mysql-8-and-more-details\/","title":{"rendered":"MySQL 8.0 \u4e2d\u7684 CTE \u529f\u80fd"},"content":{"rendered":"\n<p>CTE \u7684\u5168\u79f0\u662f Common Table Expressions\uff0c\u6709\u65f6\u5019\u4e5f\u7f29\u5199\u4e3a CTEs\uff0c\u5728 SQL \u6807\u51c6\u4e2d\u4f7f\u7528 <code>WITH clause<\/code>\u8868\u8fbe<sup>[1]<\/sup>\u3002\u5728 ANSI standard \u6807\u51c6\u7684 SQL:1999 \u4e2d\u5b9a\u4e49\uff0c\u53c2\u8003\uff1a<a href=\"https:\/\/www.iso.org\/standard\/26197.html\">ISO\/IEC 9075-2:1999<\/a><sup>[7][8]<\/sup>\u3002\u8be5\u7279\u6027\u652f\u6301\u5728 MySQL 8.0 \u4e2d\u6b63\u5f0f\u88ab\u5f15\u5165<sup>[<a href=\"https:\/\/dev.mysql.com\/blog-archive\/whats-new-in-mysql-8-0-generally-available\/\">9<\/a>]<\/sup>\u3002<\/p>\n\n\n\n<p>\u901a\u5e38\uff0c\u5982\u679c\u4e0d\u9700\u8981\u7f16\u5199\u7279\u522b\u590d\u6742\u7684 SQL \uff0c\u66f4\u4e3a\u5177\u4f53\u7684\uff0c\u5982\u679c\u65e0\u9700\u7f16\u5199\u5e26\u6709\u9012\u5f52\u529f\u80fd\u7684 SQL \u65f6\uff0cCTE \u901a\u5e38\u90fd\u4e0d\u662f\u5fc5\u987b\u7684\u3002\u6240\u4ee5\uff0c\u8be5\u8bed\u6cd5\u529f\u80fd\uff0c\u5bf9\u4e8e\u5206\u6790\u578b\u7684\u573a\u666f\u6216\u8005\u6570\u636e\u5f00\u53d1\u7b49\u573a\u666f\u66f4\u4e3a\u5e38\u7528\u3002<\/p>\n\n\n\n<p>\u8fd9\u91cc\u6982\u8ff0 CTE \u7684\u4e24\u4e2a\u5e38\u7528\u529f\u80fd\uff1a\u7b80\u5316\u5b50\u67e5\u8be2\u7ed3\u6784\u3001\u5b9e\u73b0\u9012\u5f52\u6267\u884c\u3002\u5176\u4e2d\uff0c\u5b9e\u73b0\u9012\u5f52\u6267\u884c\uff0c\u662f CTE \u72ec\u7279\u7684\u80fd\u529b\uff0c\u662f\u5bf9 SQL \u80fd\u529b\u7684\u4e00\u79cd\u8865\u5145\u3002<\/p>\n\n\n\n\n\n\n<h4 class=\"wp-block-heading\">\u7b80\u5316\u5b50\u67e5\u8be2\u7ed3\u6784<\/h4>\n\n\n\n<p>CTE \u529f\u80fd\u63d0\u4f9b\u4e86\u4e00\u79cd\u65b0\u7684\u5b50\u67e5\u8be2\u6216\u4e34\u65f6\u8868\u7684\u5199\u6cd5\u3002\u53ef\u4ee5\u628a\u540e\u7eed SQL \u4e2d\u9700\u8981\u4f7f\u7528\u7684\u4e34\u65f6\u8868\u6216\u5b50\u67e5\u8be2\u72ec\u7acb\u7684\u653e\u5728<code>WITH<\/code>\u5b50\u53e5\u4e2d\uff0c\u4f7f\u5f97\u7ed3\u6784\u770b\u8d77\u6765\u66f4\u4e3a\u6e05\u6670\u4e00\u4e9b\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\uff0c\u5982\u4e0b\u4e24\u4e2a\u5199\u6cd5\u5728\u8bed\u4e49\u4e0a\u662f\u7b49\u4ef7\u7684\uff1a<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-code\"><code lang=\"sql\" class=\"language-sql\">WITH \n  t_mid as (\n    SELECT * FROM t_1 JOIN t_2\n  )\nSELECT * FROM t_mid;<\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-code\"><code lang=\"sql\" class=\"language-sql\">SELECT * \nFROM \n  (\n    SELECT * FROM t_1 JOIN t_2\n  ) t_mid;<\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u9012\u5f52\u6267\u884c<\/h4>\n\n\n\n<p>\u8be5\u529f\u80fd\u662f CTE \u7684\u6838\u5fc3\u80fd\u529b\u3002\u4f8b\u5982\uff0c\u5728\u8868\u4e2d\u67e5\u8be2\u597d\u53cb\u7684\u597d\u53cb\u3001\u67e5\u8be2\u4e3b\u7ba1\u7684\u4e3b\u7ba1\u3001\u9012\u5f52\u6570\u636e\u751f\u6210\u7b49\u529f\u80fd\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u9012\u5f52\u6267\u884c\u65b9\u5f0f\u6982\u8ff0<\/h4>\n\n\n\n<p>\u5728\u5404\u4e2a\u6570\u636e\u5e93\u7684\u5173\u4e8e CTE \u4ecb\u7ecd\u90fd\u4f1a\u8f83\u4e3a\u8be6\u7ec6\u7684\u63cf\u8ff0\uff0c\u8fd9\u91cc\u5bf9\u8be5\u6267\u884c\u8fc7\u7a0b\u505a\u4e00\u4e2a\u7b80\u5355\u7684\u63cf\u8ff0\u3002<\/p>\n\n\n\n<p>\u5bf9\u4e8e\u9012\u5f52 CTE \u5bf9\u5e94\u7684 SQL \u603b\u662f\u6709\u7c7b\u4f3c\u7ed3\u6784\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"sql\" class=\"language-sql\">WITH RECURSIVE table_cte (col_01 ... ) AS (\n  SELECT ...                 -- return initial row set\n  UNION ALL\n  SELECT ... FROM  table_cte -- return additional row sets\n)<\/code><\/pre>\n\n\n\n<p>\u5728\u8be5\u7ed3\u6784\u4e2d\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7b2c\u4e00\u4e2a<code>SELECT<\/code>\u4e3a\u6570\u636e\u521d\u59cb\u5316<\/li>\n\n\n\n<li>\u7b2c\u4e8c\u4e2a<code>SELECT<\/code>\u5219\u4e3a\u9012\u5f52\u6267\u884c\u7684\u90e8\u5206\uff0c\u8be5\u90e8\u5206\u5728\u5f15\u7528\u8868<code>table_cte<\/code>\u65f6\uff0c\u603b\u662f\u4f7f\u7528\u8be5\u8868\u7684\u6700\u540e\u4e00\u6761\u751f\u6210\u8bb0\u5f55<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6590\u6ce2\u90a3\u5951\u6570\u5217\u7684\u751f\u6210<\/h4>\n\n\n\n<p>\u8fd9\u662f\u4e00\u4e2aMySQL\u6587\u6863\u4e2d\u7684\u793a\u4f8b<sup>[<a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.4\/en\/with.html#common-table-expressions-recursive-fibonacci-series\">10<\/a>]<\/sup>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">WITH RECURSIVE fibonacci (n, fib_n, next_fib_n) AS\n(\n  SELECT 1, 0, 1\n  UNION ALL\n  SELECT n + 1, next_fib_n, fib_n + next_fib_n\n    FROM fibonacci WHERE n &lt; 10\n)\nSELECT * FROM fibonacci;\n\n+------+-------+------------+\n| n    | fib_n | next_fib_n |\n+------+-------+------------+\n|    1 |     0 |          1 |\n|    2 |     1 |          1 |\n|    3 |     1 |          2 |\n|    4 |     2 |          3 |\n|    5 |     3 |          5 |\n...\n|   10 |    34 |         55 |\n+------+-------+------------+<\/code><\/pre>\n\n\n\n<p>\u6839\u636e\u524d\u9762\u5c0f\u7ed3\u201c\u9012\u5f52\u6267\u884c\u65b9\u5f0f\u6982\u8ff0\u201d\u4e2d\u6240\u63cf\u8ff0\uff0c\u5728\u6bcf\u6b21\u6267\u884c\u4e0a\u8ff0\u7684\u7b2c\u4e8c\u4e2a <code>SELECT<\/code>\uff0c\u5373<code>SELECT n + 1, next_fib_n, fib_n + next_fib_n FROM fibonacci<\/code>\u65f6\uff0c\u603b\u662f\u4f7f\u7528 CTE \u8868<code>fibonacci<\/code>\u6700\u540e\u751f\u6210\u7684\u8bb0\u5f55\u3002\u6240\u4ee5\uff0c\u8be5SQL\u4e2d\u5f15\u7528\u7684\u5217<code>n<\/code> \u3001<code>fib_n<\/code>\u548c<code>next_fib_n<\/code>\u5747\u4e3a\u8868<code>fibonacci<\/code>\u6700\u540e\u751f\u6210\u7684\u8bb0\u5f55\uff0c\u5373\u4e0a\u4e00\u6761\u8bb0\u5f55\u3002<\/p>\n\n\n\n<p>\u6240\u4ee5\uff0c\u8be5 SQL \u7b2c\u4e00\u5217\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u81ea\u589e\u7684\u5e8f\u5217\uff1b\u7b2c\u4e8c\u5217\uff0c\u5373\u4e3a\u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1b\u7b2c\u4e09\u5217\u662f\u7528\u4e8e\u8ba1\u7b97\u7684\u4e2d\u95f4\u5217\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u5e7f\u5ea6\u4f18\u5148\u8fd8\u662f\u6df1\u5ea6\u4f18\u5148<\/h4>\n\n\n\n<p>\u5728 MySQL \u7684\u9012\u5f52\u904d\u5386\u4e2d\uff0c\u5e76\u6ca1\u6709\u50cf PostgreSQL \u90a3\u6837\u53ef\u4ee5\u901a\u8fc7 SQL \u5173\u952e\u5b57\u53bb\u63a7\u5236\u3002\u5728\u6587\u6863\u4e2d\uff0c\u4e5f\u6ca1\u6709\u63cf\u8ff0\u662f\u5e7f\u5ea6\u4f18\u5148\u8fd8\u662f\u6df1\u5ea6\u4f18\u5148\u3002\u8fd9\u91cc\u901a\u8fc7\u4e00\u4e9b SQL \u53d8\u91cf\u6765\u770b\u770b\uff0c\u5230\u5e95\u662f\u5e7f\u5ea6\u4f18\u5148\u8fd8\u662f\u6df1\u5ea6\u4f18\u5148\u3002<\/p>\n\n\n\n<p>\u5148\u8bf4\u7ed3\u8bba\u5427\uff1aMySQL \u4f7f\u7528\u7684\u662f\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u3002\u5173\u6ce8\u9a8c\u8bc1\u8fc7\u7a0b\u7684\uff0c\u53ef\u4ee5\u7ee7\u7eed\u9605\u8bfb\u8fd9\u4e00\u6bb5\u7684\u793a\u4f8b\u548c\u89e3\u8bfb\u3002<\/p>\n\n\n\n<p>\u8fd9\u91cc\u4f7f\u7528 MySQL \u5b98\u65b9\u6587\u6863\u4e2d\u53e6\u4e00\u4e2a\u793a\u4f8b\u3002\u8be5\u793a\u4f8b\u5305\u542b\u4e00\u4e2a\u4f01\u4e1a\u4eba\u5458\u7ec4\u7ec7\u67b6\u6784\u7684\u5173\u7cfb\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"sql\" class=\"language-sql\">CREATE TABLE employees (\n  id         INT PRIMARY KEY NOT NULL,\n  name       VARCHAR(100) NOT NULL,\n  manager_id INT NULL,\n  INDEX (manager_id),\nFOREIGN KEY (manager_id) REFERENCES employees (id)\n);\nINSERT INTO employees VALUES\n(333, \"Yasmina\", NULL),  # Yasmina is the CEO (manager_id is NULL)\n(198, \"John\", 333),      # John has ID 198 and reports to 333 (Yasmina)\n(692, \"Tarek\", 333),\n(29, \"Pedro\", 198),\n(4610, \"Sarah\", 29),\n(72, \"Pierre\", 29),\n(123, \"Adil\", 692);<\/code><\/pre>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u7684\u9012\u5f52 CTE \u8bed\u53e5\u8fdb\u884c\u7ec4\u7ec7\u67b6\u6784\u904d\u5386\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"sql\" class=\"language-sql\">WITH RECURSIVE employee_paths (id, name, path) AS\n(\n  SELECT id, name, CAST(id AS CHAR(200))\n    FROM employees\n    WHERE manager_id IS NULL\n  UNION ALL\n  SELECT e.id, e.name, CONCAT(ep.path, ',', e.id)\n    FROM employee_paths AS ep JOIN employees AS e\n      ON ep.id = e.manager_id\n)\nSELECT * FROM employee_paths ORDER BY path;<\/code><\/pre>\n\n\n\n<p>\u6211\u4eec\u5bf9\u8fd9\u6761 SQL \u8bed\u53e5\u8fdb\u884c\u5982\u4e0b\u4fee\u6539\uff0c\u4ee5\u89c2\u5bdf\u662f\u5e7f\u5ea6\u4f18\u5148\u8fd8\u662f\u6df1\u5ea6\u4f18\u5148\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"sql\" class=\"language-sql\">WITH RECURSIVE employee_paths (id, name, path,seq) AS\n(\n  SELECT id, name, CAST(id AS CHAR(200)),@seq:=1\n    FROM employees\n    WHERE manager_id IS NULL\n  UNION ALL\n  SELECT e.id, e.name, CONCAT(ep.path, ',', e.id),@seq:=@seq+1\n    FROM employee_paths AS ep JOIN employees AS e\n      ON ep.id = e.manager_id\n)\nSELECT * FROM employee_paths ORDER BY seq;\n\n+------+---------+-----------------+------+\n| id   | name    | path            | seq  |\n+------+---------+-----------------+------+\n|  333 | Yasmina | 333             |    1 |\n|  198 | John    | 333,198         |    2 |\n|  692 | Tarek   | 333,692         |    3 |\n|   29 | Pedro   | 333,198,29      |    4 |\n|  123 | Adil    | 333,692,123     |    5 |\n|   72 | Pierre  | 333,198,29,72   |    6 |\n| 4610 | Sarah   | 333,198,29,4610 |    7 |\n+------+---------+-----------------+------+<\/code><\/pre>\n\n\n\n<p>\u8be5\u7ed3\u679c\u7684\u6700\u540e\u4e00\u5217 seq\uff0c\u53cd\u6620\u4e86\u8f93\u51fa\u7684\u5148\u540e\u987a\u5e8f\u3002\u53ef\u4ee5\u770b\u5230\uff0c\u8fd9\u91cc\u4f7f\u7528\u7684\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u53c2\u8003\u94fe\u63a5<\/h4>\n\n\n\n<ul class=\"wp-block-list is-style-no-disc\">\n<li class=\"has-x-small-font-size\">[1] <a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.4\/en\/with.html\">15.2.20 WITH (Common Table Expressions)@MySQL<\/a> <\/li>\n\n\n\n<li class=\"has-x-small-font-size\">[2] <a href=\"https:\/\/www.postgresql.org\/docs\/current\/queries-with.html\">7.8. WITH Queries (Common Table Expressions)@PostgreSQL<\/a><\/li>\n\n\n\n<li class=\"has-x-small-font-size\">[3] <a href=\"https:\/\/learn.microsoft.com\/en-us\/sql\/t-sql\/queries\/with-common-table-expression-transact-sql?view=sql-server-ver16\">WITH common_table_expression (Transact-SQL)@SQL Server<\/a><\/li>\n\n\n\n<li class=\"has-x-small-font-size\">[4] <a href=\"https:\/\/docs.oracle.com\/cd\/E11882_01\/server.112\/e41084\/statements_10002.htm#BCEJGIBG\">subquery_factoring_clause@Oracle Documentatioin<\/a> <\/li>\n\n\n\n<li class=\"has-x-small-font-size\">[5] <a href=\"https:\/\/en.wikipedia.org\/wiki\/Hierarchical_and_recursive_queries_in_SQL#Common_table_expression\">Hierarchical and recursive queries in SQL@Wikipedia<\/a><\/li>\n\n\n\n<li class=\"has-x-small-font-size\">[6] <a href=\"https:\/\/docs.aws.amazon.com\/dms\/latest\/sql-server-to-aurora-postgresql-migration-playbook\/chap-sql-server-aurora-pg.sql.cte.html\">Common table expressions for ANSI SQL@AWS Documentation<\/a> <\/li>\n\n\n\n<li class=\"has-x-small-font-size\">[7] <a href=\"https:\/\/www.iso.org\/standard\/26197.html\">https:\/\/www.iso.org\/standard\/26197.html<\/a> <\/li>\n\n\n\n<li class=\"has-x-small-font-size\">[8] <a href=\"https:\/\/cdn.standards.iteh.ai\/samples\/26197\/1c06f2924d084a2a9f3ec0cc6aea0bf3\/ISO-IEC-9075-2-1999.pdf\">Information technology \u2014 Database languages \u2014 SQL \u2014 Part 2: Foundation (SQL\/Foundation)<\/a><\/li>\n\n\n\n<li class=\"has-x-small-font-size\">[9] <a href=\"https:\/\/dev.mysql.com\/blog-archive\/whats-new-in-mysql-8-0-generally-available\/\">https:\/\/dev.mysql.com\/blog-archive\/whats-new-in-mysql-8-0-generally-available\/<\/a><\/li>\n\n\n\n<li class=\"has-x-small-font-size\">[10] <a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.4\/en\/with.html#common-table-expressions-recursive-fibonacci-series\">https:\/\/dev.mysql.com\/doc\/refman\/8.4\/en\/with.html#common-table-expressions-recursive-fibonacci-series<\/a> <\/li>\n<\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u7bc7\u6587\u7ae0\u8865\u5145\u4ecb\u7ecd\u4e86 MySQL 8.0 \u4e2d\u7684 CTE\u529f\u80fd\uff0c\u5e76\u9a8c\u8bc1\u4e86\u8be5\u529f\u80fd\u5728\u904d\u5386\u65f6\u4f7f\u7528\u4e86\u5e7f\u5ea6\u4f18\u5148\u7684\u641c\u7d22<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_eb_attr":"","inline_featured_image":false,"_tocer_settings":[],"footnotes":""},"categories":[6],"tags":[],"class_list":["post-16701","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/16701","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/comments?post=16701"}],"version-history":[{"count":20,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/16701\/revisions"}],"predecessor-version":[{"id":16897,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/16701\/revisions\/16897"}],"wp:attachment":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/media?parent=16701"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/categories?post=16701"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/tags?post=16701"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}