{"id":11644,"date":"2020-08-15T10:53:27","date_gmt":"2020-08-15T01:53:27","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=11644"},"modified":"2023-05-27T23:15:48","modified_gmt":"2023-05-27T14:15:48","slug":"%e3%80%90sql%e3%80%91with%e5%8f%a5%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=11644","title":{"rendered":"\u3010SQL\u3011\u8aad\u307f\u3084\u3059\u3044SQL\u3092\u4f5c\u308b\u305f\u3081\u306e\u624b\u6cd5\uff08WITH\u53e5\uff09"},"content":{"rendered":"<h2>\u57fa\u672c\u6307\u91dd<\/h2>\n<h3>\u300c\u8907\u96d1\u306aSQL\u300d\u304b\u3089\u9003\u3052\u306a\u3044\u3053\u3068<\/h3>\n<p>\u8907\u96d1\u306aSQL\u306f\u907f\u3051\u305f\u304c\u308b\u73fe\u5834\u3082\u3042\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002\u3057\u304b\u3057\u3001\u8907\u96d1\u306aSQL\u3092\u4f7f\u308f\u306a\u3044\u3068\u30eb\u30fc\u30d7\u304b\u3089\u306eDB\u547c\u3073\u51fa\u3057\u306e\u591a\u91cd\u30eb\u30fc\u30d7\u304c\u767a\u751f\u3059\u308b\u305f\u3081\u3001\u30a2\u30d7\u30ea\u306e\u6027\u80fd\u304c\u9045\u304f\u306a\u3063\u3066\u3057\u307e\u3044\u307e\u3059\u3002\u8907\u96d1\u306aSQL\u304b\u3089\u9003\u3052\u306a\u3044\u3088\u3046\u306b\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<h2>\u8aad\u307f\u3084\u3059\u304f\u3059\u308b\u306b\u306f\uff1f<\/h2>\n<p>\u305f\u3060\u3001\u8aad\u307f\u3065\u3089\u304f\u306a\u308b\u306e\u306f\u554f\u984c\u3067\u3059\u3002\u305d\u306e\u5834\u5408\u306f\u30b5\u30d6\u30af\u30a8\u30ea\u3092<span style=\"color: #ff0000;\"><strong>WITH\u53e5<\/strong><\/span>\u3092\u4f7f\u3046\u3088\u3046\u306b\u3057\u307e\u3057\u3087\u3046\u3002WITH\u53e5\u3092\u4f7f\u3046\u3053\u3068\u3067\u300c\u30af\u30a8\u30ea\u5185\u306b\u65b0\u305f\u306a\u30c6\u30fc\u30d6\u30eb\u300d\u3092\u4f5c\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\uff08\u30b5\u30d6\u30af\u30a8\u30ea\u306b\u540d\u524d\u3092\u3064\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u6a5f\u80fd\u3067\u3059\u3002\uff09<\/p>\n<p>\u307e\u305f\u3001\u4e00\u5ea6\u5b9a\u7fa9\u3059\u308c\u3070\u4f55\u5ea6\u3082\u4f7f\u3044\u56de\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<h3>MySQL\u3067\u306f\uff1f<\/h3>\n<p>Web\u696d\u754c\u306a\u3069\u3067\u306fMySQL\u3092\u4f7f\u3046\u3053\u3068\u304c\u591a\u3044\u3067\u3057\u3087\u3046\u3002\u305f\u3060\u3001<span style=\"color: #ff0000;\"><strong>MySQL\u306f8.0\u4ee5\u964d\u3057\u304bWITH\u53e5\u306b\u5bfe\u5fdc\u3057\u3066\u3044\u306a\u3044<\/strong><\/span>\u306e\u3067\u53b3\u3057\u3044\u3067\u3059\u3002\uff08RDS\u306eAurora\u304c5.7\u306b\u3057\u304b\u5bfe\u5fdc\u3057\u3066\u306a\u3044\u306e\u3067\u73fe\u72b6\u306f8.0\u306f\u3042\u307e\u308a\u4f7f\u308f\u308c\u3066\u3044\u306a\u3044\u3002\uff09<\/p>\n<p>\u3069\u3046\u3057\u3066\u3082\u30015.7\u4ee5\u4e0b\u306e\u5834\u5408\u3067\u3082\u30b3\u30fc\u30c9\u306e\u53ef\u8aad\u6027\u3092\u4e0a\u3052\u305f\u3044\u5834\u5408\u306fVIEW\u306a\u3069\u3092\u4f7f\u3046\u306e\u3082\u9078\u629e\u80a2\u306e\u4e00\u3064\u3067\u3059\u3002<\/p>\n<h2>WITH\u53e5\u306e\u57fa\u672c\u69cb\u6587<\/h2>\n<pre class=\"lang:default decode:true\">WITH\u3000\u2019\u65b0\u305f\u306b\u4f5c\u6210\u3057\u305f\u30c6\u30fc\u30d6\u30eb\u540d\u2019as\r\n\u3000\uff08\u4f5c\u6210\u3057\u305f\u3044\u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u308b\u30af\u30a8\u30ea\r\n);<\/pre>\n<h3>\u4f8b<\/h3>\n<pre class=\"lang:default decode:true \">-- WITH\u53e5\u3092\u4f7f\u3063\u305f\u30c6\u30fc\u30d6\u30eb\u5b9a\u7fa9\r\nWITH aaa as (select * from user where name = \"\u592a\u90ce\")\r\n\r\n-- WITH\u53e5\u3092\u4f7f\u3046\u3002\r\nselect * from aaa:<\/pre>\n<h2>RECURSIVE\u3092\u7528\u3044\u305fWITH\u53e5<\/h2>\n<p>\u3053\u306e\u5f62\u3092\u3068\u308a\u307e\u3059\u3002\u9023\u756a\u306e\u3088\u3046\u306a\u7d50\u679c\u51fa\u529b\u306b\u30eb\u30fc\u30d7\u51e6\u7406\u304c\u5fc5\u8981\u306a\u3082\u306e\u3092\u4f5c\u308b\u969b\u306b\u4fbf\u5229\u3067\u3059\u3002<\/p>\n<pre class=\"\">WITH RECURSIVE hoge AS (\r\n    FROM\u53e5\u306bhoge\u3092\u542b\u307e\u306a\u3044SELECT\u6587\uff08\u975e\u518d\u5e30\u9805\uff09\r\n    UNION ALL\u307e\u305f\u306fUNION\r\n\u3000\u3000 FROM\u53e5\u306bhoge\u3092\u542b\u3080SELECT\u6587\uff08\u518d\u5e30\u9805\uff09\r\n)\r\n\r\nWITH\u53e5\u3092\u4f7f\u7528\u3057\u305fSELECT\u6587<\/pre>\n<h3>FROM\u53e5\u306bhoge\u3092\u542b\u307e\u306a\u3044SELECT\u6587<\/h3>\n<p>\u975e\u518d\u5e30\u9805\u3068\u547c\u3073\u307e\u3059\u3002<\/p>\n<h3>FROM\u53e5\u306bhoge\u3092\u542b\u3080SELECT\u6587<\/h3>\n<p>\u518d\u5e30\u9805\u3001\u4f5c\u696d\u7528\u306e\u30c6\u30fc\u30d6\u30eb\u3068\u3082\u3044\u3048\u307e\u3059\u3002<\/p>\n<h2>\u51e6\u7406\u306e\u6d41\u308c<\/h2>\n<pre class=\"lang:default decode:true \">1.\u300c\u975e\u518d\u5e30\u9805\u300d\u3092\u8a08\u7b97\u3057\u3001\u7d50\u679c\u3092\u4f5c\u696d\u30c6\u30fc\u30d6\u30eb\u3068\u547c\u3070\u308c\u308b\u4e00\u6642\u7684\u306a\u8868\u306b\u683c\u7d0d\u3059\u308b\r\n2.1.\u306e\u300c\u518d\u5e30\u9805\u300d\u306e\u8a08\u7b97\u7d50\u679c\u3092\u975e\u518d\u5e30\u9805\u306eFROM\u53e5\u306b\u4ee3\u5165\u3057\u3066SQL\u306e\u5b9f\u884c\u7d50\u679c\u304c\u7a7a\u306b\u306a\u308b\u307e\u3067\u518d\u5ea6\u8a08\u7b97\u3057\u307e\u3059\u3002\r\n3.2.\u3067\u8a08\u7b97\u3057\u305f\u7d50\u679c\u3092\u5168\u3066UNION\u307e\u305f\u306fUNION ALL\u3067\u8db3\u3057\u5408\u308f\u305b\u307e\u3059\u3002<\/pre>\n<p>\u5b9f\u969b\u306b\u3084\u3063\u3066\u3044\u308b\u3053\u3068\u3068\u3057\u3066\u306f\u300c\u518d\u5e30\u300d\u3068\u3044\u3046\u3088\u308a\u306f\u300c\u53cd\u5fa9\u51e6\u7406\u300d\u3068\u8a00\u3048\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u57fa\u672c\u6307\u91dd \u300c\u8907\u96d1\u306aSQL\u300d\u304b\u3089\u9003\u3052\u306a\u3044\u3053\u3068 \u8907\u96d1\u306aSQL\u306f\u907f\u3051\u305f\u304c\u308b\u73fe\u5834\u3082\u3042\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002\u3057\u304b\u3057\u3001\u8907\u96d1\u306aSQL\u3092\u4f7f\u308f\u306a\u3044\u3068\u30eb\u30fc\u30d7\u304b\u3089\u306eDB\u547c\u3073\u51fa\u3057\u306e\u591a\u91cd\u30eb\u30fc\u30d7\u304c\u767a\u751f\u3059\u308b\u305f\u3081\u3001\u30a2\u30d7\u30ea\u306e\u6027\u80fd\u304c\u9045\u304f\u306a\u3063\u3066\u3057\u307e\u3044\u307e\u3059\u3002\u8907\u96d1\u306a [&hellip;]","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[24],"tags":[],"_links":{"self":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/11644"}],"collection":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=11644"}],"version-history":[{"count":7,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/11644\/revisions"}],"predecessor-version":[{"id":20586,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/11644\/revisions\/20586"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=11644"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=11644"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=11644"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}