{"id":5370,"date":"2020-07-29T23:50:39","date_gmt":"2020-07-29T15:50:39","guid":{"rendered":"http:\/\/39.97.231.10\/?p=5370"},"modified":"2020-07-29T23:50:39","modified_gmt":"2020-07-29T15:50:39","slug":"mysql8-0%e6%96%b0%e7%89%b9%e6%80%a7ctecommon-table-expression","status":"publish","type":"post","link":"http:\/\/www.weijing.co\/?p=5370","title":{"rendered":"MySQL8.0\u65b0\u7279\u6027CTE(Common Table Expression)"},"content":{"rendered":"<div>\n<div>\n<div>\nCTE(Common Table Expression)\u53ef\u4ee5\u8ba4\u4e3a\u662f\u6d3e\u751f\u8868(derived table)\u7684\u66ff\u4ee3\uff0c\u5728\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\uff0cCTE\u7b80\u5316\u4e86\u590d\u6742\u7684join\u67e5\u8be2\u548c\u5b50\u67e5\u8be2\uff0c\u63d0\u9ad8\u4e86SQL\u7684\u53ef\u8bfb\u6027\u548c\u6267\u884c\u6027\u80fd\u3002CTE\u662fANSI SQL 99\u6807\u51c6\u7684\u4e00\u90e8\u5206\uff0c\u5728MySQL 8.0.1\u7248\u672c\u88ab\u5f15\u5165\u3002<\/p>\n<h4>1. CTE\u4f18\u52bf<\/h4>\n<ul>\n<li>\u67e5\u8be2\u8bed\u53e5\u7684\u53ef\u8bfb\u6027\u66f4\u597d<\/li>\n<li>\u5728\u4e00\u4e2a\u67e5\u8be2\u4e2d\uff0c\u53ef\u4ee5\u88ab\u5f15\u7528\u591a\u6b21<\/li>\n<li>\u80fd\u591f\u94fe\u63a5\u591a\u4e2aCTE<\/li>\n<li>\u80fd\u591f\u521b\u5efa\u9012\u5f52\u67e5\u8be2<\/li>\n<li>\u80fd\u591f\u63d0\u9ad8SQL\u6267\u884c\u6027\u80fd<\/li>\n<li>\u80fd\u591f\u6709\u6548\u5730\u66ff\u4ee3\u89c6\u56fe<\/li>\n<\/ul>\n<h4>2. \u5982\u4f55\u521b\u5efa\u548c\u4f7f\u7528CTE<\/h4>\n<p>CTE\u7c7b\u4f3c\u4e8e\u4f7f\u7528\u5b50\u67e5\u8be2\u65f6\u7684\u6d3e\u751f\u8868\uff0c\u4f46\u662fCTE\u7684\u5b9a\u4e49\u4e0d\u5728SQL\u4e3b\u4f53\u4e2d\uff0c\u800c\u662f\u63d0\u5230SQL\u6700\u524d\u7aef\uff0c\u58f0\u660eCTE\u7684\u9700\u8981\u4f7f\u7528\u8bed\u6cd5WITH\u3002<\/p>\n<h5>2.1 CTE\u7684\u4f7f\u7528<\/h5>\n<p>\u5148\u770b\u4e00\u4e2a\u6d3e\u751f\u8868\u5b9e\u73b0\u7684\u4f8b\u5b50\uff1a<\/p>\n<pre>mysql&gt; SELECT Name, Population FROM (SELECT * FROM country WHERE continent=\\'Europe\\') AS derived_t ORDER BY Population DESC LIMIT 5;\n<\/pre>\n<p>\u4f7f\u7528CTE\u6539\u5199\u540e\uff0cSQL\u53d8\u6210\u8fd9\u6837\uff1a<\/p>\n<pre>mysql&gt; WITH cte AS (SELECT * FROM country WHERE continent=\\'Europe\\') SELECT Name, Population FROM cte ORDER BY Population DESC LIMIT 5;\n<\/pre>\n<p>CTE\u7684\u8bed\u6cd5\u4e5f\u6bd4\u8f83\u7b80\u5355\uff0c\u5728SQL\u4e3b\u4f53\u67e5\u8be2\u4e4b\u524d\uff0c\u4f7f\u7528WITH\u8bed\u6cd5\uff0c\u5b9a\u4e49\u4e00\u4e2a\u6216\u8005\u591a\u4e2aCTE\uff0c\u7136\u540e\u5c31\u53ef\u4ee5\u5728\u67e5\u8be2SQL\u7684\u4e3b\u4f53\u4e2d\u5f15\u7528\u4e00\u6b21\u6216\u591a\u6b21CTE\uff0c\u53ef\u4ee5\u628aCTE\u770b\u6210\u662f\u4e00\u7c7b\u63d0\u524d\u7269\u5316\u7684\u4e34\u65f6\u8868\uff0c\u4ee5\u4fbf\u4e8e\u67e5\u8be2\u4e3b\u4f53\u5f15\u7528\u3002<\/p>\n<h5>2.2 \u4e3aCTE\u6307\u5b9a\u5177\u4f53\u7684\u5b57\u6bb5\u540d\u79f0<\/h5>\n<p>\u4f7f\u7528\u5706\u62ec\u53f7\u4e3aCTE\u6307\u5b9a\u5b57\u6bb5\u540d\u79f0\uff0c\u5982\u4e0beur_name\u548ceur_population\u4e3aCTE\u7684\u5b57\u6bb5\uff1a<\/p>\n<pre>mysql&gt; WITH cte(eur_name, eur_population) AS (SELECT Name, Population FROM country WHERE continent=\\'Europe\\')\n   SELECT eur_name, eur_population FROM cte\n   ORDER BY eur_opulation DESC LIMIT 5;\n<\/pre>\n<h5>2.3 CTE\u4e5f\u53ef\u4ee5\u88ab\u7528\u4f5c\u6570\u636e\u6e90\u6765\u66f4\u65b0\u5176\u4ed6\u8868<\/h5>\n<p>CTE\u53ef\u4ee5\u4f5c\u4e3a\u6570\u636e\u6e90\uff0c\u6765\u66f4\u65b0\u6216\u8005\u5220\u9664\u5176\u4ed6\u8868\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre>mysql&gt; WITH cte(eur_code, eur_population) AS (SELECT Code, Population FROM country WHERE continent=\\'Europe\\')\n    UPDATE country_2020, cte\n    SET Population_2020 = ROUND(eur_population*1.1)\n    WHERE Code=cte.eur_code;\nmysql&gt; WITH cte AS (SELECT Code FROM country WHERE continent &lt;&gt; \\'Europe\\')\n    DELETE country_2020\n    FROM country_2020, cte\n    WHERE country_2020.Code=cte.Code;\n<\/pre>\n<h5>2.4 CTE\u4e5f\u53ef\u4ee5\u7528\u4e8einsert &#8230; select \u8bed\u53e5<\/h5>\n<pre>mysql&gt; INSERT INTO largest_countries\n    WITH cte AS (SELECT Code, Name, SurfaceArea FROM country ORDER BY SurfaceArea DESC LIMIT 10)\n    SELECT * FROM cte;\n<\/pre>\n<h5>2.5 CTE\u4f5c\u4e3a\u63d0\u524d\u7269\u5316\u7684\u4e34\u65f6\u8868<\/h5>\n<p>\u5b9a\u4e49\u591a\u4e2aCTE\uff0c\u4f5c\u4e3a\u63d0\u524d\u7269\u5316\u7684\u4e34\u65f6\u8868\uff0c\u5728\u4e3b\u67e5\u8be2\u91cc\u9762\u53ef\u4ee5\u591a\u6b21\u5f15\u7528\u8fd9\u4e9b\u4e34\u65f6\u8868\u3002\u5982\u4e0b\uff1a<\/p>\n<pre>WITH cte1 AS (SELECT ... FROM ... WHERE ...),\n  \u00a0 \u00a0     cte2 AS (SELECT ... FROM ... WHERE ...)\nSELECT ...\nFROM table1, table1, cte1, cte2 ....\nWHERE .....\n<\/pre>\n<h5>2.6 CTE\u7684\u53ef\u89c1\u6027<\/h5>\n<p>\u4e0b\u9762\u4e24\u4e2a\u4f8b\u5b50\uff0c\u7b2c\u4e00\u4e2a\u4f8b\u5b50\u4e2dcte\u5bf9\u4e8e\u9876\u5c42SELECT\u53ef\u89c1\uff0c\u7b2c\u4e8c\u4e2a\u4f8b\u5b50\u4e2d\uff0ccte\u5bf9\u9876\u5c42SELECT\u4e0d\u53ef\u89c1\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u4e0d\u53ef\u89c1\u7684\u95ee\u9898\uff0c\u901a\u5e38\u5c06CTE\u5b9a\u4e49\u5728\u6700\u524d\u9762\uff0c\u4ee5\u4fbf\u80fd\u591f\u5728\u67e5\u8be2\u4e3b\u4f53\u7684\u4efb\u4f55\u5730\u65b9\u90fd\u80fd\u5f15\u7528\u5230CTE\u3002<\/p>\n<pre>WITH cte AS (SELECT Code FROM country WHERE Population&lt;1000000)\nSELECT * FROM city WHERE city.CountryCode IN\n(SELECT Code FROM cte);\u00a0\u00a0 # cte\u5bf9\u4e8e\u9876\u5c42SELECT\u662f\u53ef\u89c1\u7684\nSELECT * FROM city WHERE city.CountryCode IN\n(WITH cte AS (SELECT Code FROM country WHERE Population&lt;1000000)\nSELECT Code from cte);\u00a0\u00a0 # cte\u5bf9\u4e8e\u9876\u5c42SELECT\u4e0d\u53ef\u89c1\n<\/pre>\n<h5>2.7 CTE\u5f15\u7528\u94fe<\/h5>\n<p>\u5982\u679c\u5728\u4e00\u4e2a\u67e5\u8be2\u4e2d\u521b\u5efa\u591a\u4e2aCTE\uff0c\u53ef\u80fd\u4f1a\u51fa\u73b0\u4e00\u4e2aCTE\u5f15\u7528\u524d\u4e00\u4e2aCTE\uff0c\u5bfc\u81f4CTE\u5f15\u7528\u94fe\u7684\u4ea7\u751f\u3002\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\u5c55\u793a\u4e86CTE\u5f15\u7528\u94fe\uff1a<\/p>\n<pre>WITH density_by_country(country,density) AS\n       (SELECT Name, Population\/SurfaceArea\n       FROM country\n       WHERE Population&gt;0 and surfacearea&gt;0),\n       max_density(country,maxdensity,label) AS\n       (SELECT country, density, \\'max density\\'\n       FROM density_by_country\n       WHERE density=(SELECT MAX(density) FROM density_by_country)),\n       min_density(country,mindensity,label) AS\n       (SELECT country, density, \\'min density\\'\n       FROM density_by_country\n       WHERE density=(SELECT MIN(density) FROM density_by_country))\n       SELECT * FROM max_density UNION ALL SELECT * FROM min_density;\n<\/pre>\n<p>\u4e0a\u8ff0SQL\u5982\u679c\u4f7f\u7528\u6d3e\u751f\u8868\u7684\u65b9\u5f0f\u6539\u5199\uff0c\u4e5f\u5c06\u662f\u975e\u5e38\u5e9e\u5927\u548c\u590d\u6742\u7684\u3002<\/p>\n<h5>2.8 \u4f7f\u7528CTE\u4ee3\u66ff\u89c6\u56fe<\/h5>\n<p>\u5982\u679c\u4f60\u7684\u7528\u6237\u6ca1\u6709\u6743\u9650\u521b\u5efa\u89c6\u56fe\uff0c\u800c\u540c\u65f6\u53c8\u6709\u9700\u8981\u4f7f\u7528\u89c6\u56fe\uff0c\u4e0d\u59a8\u8bd5\u8bd5CTE\u6765\u4ee3\u66ff\u89c6\u56fe\u3002<\/p>\n<pre>mysql&gt; CREATE VIEW city_pop_by_country AS (SELECT countrycode, SUM(population) sum_population FROM city GROUP BY countrycode);\nmysql&gt; SELECT name, city_pop_by_country.sum_population\/country.population ratio\n       FROM country, city_pop_by_country\n       WHERE country.code=city_pop_by_country.countrycode\n         AND country.population &gt; (SELECT 10*AVG(sum_population) FROM city_pop_by_country);\n<\/pre>\n<p>\u89c6\u56fe\u6539\u5199\u4e3aCTE\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre>WITH city_pop_by_country AS (SELECT countrycode, SUM(population) sum_population FROM city GROUP BY countrycode)\n       SELECT name, city_pop_by_country.sum_population\/country.population ratio\n       FROM country, city_pop_by_country\n       WHERE country.code=city_pop_by_country.countrycode\n         AND country.population &gt; (SELECT 10*AVG(sum_population) FROM city_pop_by_country);\n<\/pre>\n<p>\u4f7f\u7528CTE\u4ee3\u66ff\u89c6\u56fe\u80fd\u591f\u6709\u6548\u63d0\u9ad8\u6267\u884c\u6548\u7387\uff0c\u5728\u672c\u6848\u4f8b\u4e2d\uff0c\u89c6\u56fe\u7684\u6267\u884c\u65f6\u95f4\u5927\u6982\u662f0.0097\u79d2\uff0c\u800cCTE\u5927\u6982\u662f0.0054\u79d2\uff0cCTE\u66f4\u5feb\uff0c\u56e0\u4e3a\u53ea\u9700\u8981\u4e00\u6b21\u7269\u5316\u4e34\u65f6\u8868\uff0c\u53ef\u4ee5\u88ab\u591a\u6b21\u5f15\u7528\u3002<\/p>\n<h4>3. \u603b\u7ed3<\/h4>\n<p>\u5728MySQL 8.0 \u4e2d\u5f15\u5165CTE\u65b0\u7279\u6027\uff0c\u5728\u5927\u591a\u6570\u573a\u666f\u4e0b\uff0c\u80fd\u591f\u7b80\u5316SQL\uff0c\u63d0\u9ad8\u53ef\u8bfb\u6027\uff0c\u540c\u65f6\u4e5f\u80fd\u4f7f\u7528CTE\u4ee3\u66ff\u89c6\u56fe\uff0c\u63d0\u9ad8\u6574\u4f53\u6027\u80fd\u3002\u53e6\u5916CTE\u4e5f\u80fd\u5b9e\u73b0\u9012\u5f52\u67e5\u8be2\uff0c\u4e0b\u4e00\u7bc7\u6587\u7ae0\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u3002\n<\/p><\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>CTE(Common Table Expression)\u53ef\u4ee5\u8ba4\u4e3a\u662f\u6d3e\u751f\u8868(derived table)\u7684\u66ff\u4ee3\uff0c &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"http:\/\/www.weijing.co\/?p=5370\"> <span class=\"screen-reader-text\">MySQL8.0\u65b0\u7279\u6027CTE(Common Table Expression)<\/span> \u67e5\u770b\u5168\u6587 &raquo;<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":""},"categories":[90,88],"tags":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"mailpoet_newsletter_max":false,"woocommerce_thumbnail":false,"woocommerce_single":false,"woocommerce_gallery_thumbnail":false,"shop_catalog":false,"shop_single":false,"shop_thumbnail":false},"uagb_author_info":{"display_name":"weijing","author_link":"http:\/\/www.weijing.co\/?author=4"},"uagb_comment_info":0,"uagb_excerpt":"CTE(Common Table Expression)\u53ef\u4ee5\u8ba4\u4e3a\u662f\u6d3e\u751f\u8868(derived table)\u7684\u66ff\u4ee3\uff0c&hellip;","_links":{"self":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/5370"}],"collection":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5370"}],"version-history":[{"count":0,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/5370\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5370"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}