{"id":156,"date":"2018-07-05T23:40:03","date_gmt":"2018-07-05T15:40:03","guid":{"rendered":"http:\/\/shanlin.info\/?p=156"},"modified":"2018-07-05T23:42:26","modified_gmt":"2018-07-05T15:42:26","slug":"mysql%e5%ae%9e%e7%8e%b0%e7%a7%bb%e5%8a%a8%e5%b9%b3%e5%9d%87%e8%ae%a1%e7%ae%97","status":"publish","type":"post","link":"https:\/\/shanlin.info\/?p=156","title":{"rendered":"MySQL\u5b9e\u73b0\u79fb\u52a8\u5e73\u5747\u8ba1\u7b97"},"content":{"rendered":"<p>\u6211\u4eec\u6709\u8fd9\u6837\u7684\u4e00\u4e2a\u6570\u636e\u8868test_sales\uff0c\u8bb0\u5f55\u4e86\u6bcf\u65e5\u7684\u9500\u552e\u4ea7\u54c1\u7684\u9500\u552e\u6570\u91cf\u6570\u636e\u3002\u8868\u4e2ddate\u8868\u793a\u9500\u552e\u65e5\u671f\uff0cnum\u8868\u793a\u9500\u552e\u6570\u91cf\uff0cavg_5\u5c31\u662f\u60f3\u8981\u83b7\u5f97\u76845\u65e5\u79fb\u52a8\u5e73\u5747\u9500\u552e\u91cf\u6570\u636e\uff08\u8fd9\u91cc\u76f4\u63a5\u5217\u51fa\u662f\u65b9\u4fbf\u540e\u671f\u8ba1\u7b97\u7ed3\u679c\u9a8c\u8bc1\uff09\u3002<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/shanlin.info\/wp-content\/uploads\/2018\/07\/070518_1540_MySQL1.png\" alt=\"\" \/><\/p>\n<p>Emmm\uff0c\u636e\u8bf4oracle\u6709\u7a97\u53e3\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u51fa\u7ed3\u679c\uff0c\u4f53\u73b0\u51fa\u4e86\u4ed8\u8d39\u4ea7\u54c1\u7684\u4f18\u52bf\u3002Mysql\u5219\u53ef\u4ee5\u901a\u8fc7\u5173\u8054\u8868\u672c\u8eab\uff0c\u521b\u9020\u8f85\u52a9\u5217\u7684\u529e\u6cd5\u6765\u89e3\u51b3\u3002<\/p>\n<p>\u5148\u770b\u4ee3\u7801\uff1a<\/p>\n<ol>\n<li>\n<div style=\"background: white;\"><span style=\"color: #006699; font-family: Consolas; font-size: 9pt;\"><strong>SELECT<\/strong><span style=\"color: black;\">\u00a0t.<span style=\"color: #006699;\"><strong>date<\/strong><span style=\"color: black;\">,t.num,t.avg_5,\u00a0\u00a0<span style=\"color: #5c5c5c;\"><br \/>\n<\/span><\/span><\/span><\/span><\/span><\/div>\n<\/li>\n<li>\n<div style=\"background: #f8f8f8;\"><span style=\"color: deeppink; font-family: Consolas; font-size: 9pt;\">case<span style=\"color: black;\">\u00a0<span style=\"color: #006699;\"><strong>when<\/strong><span style=\"color: black;\">\u00a0t.<span style=\"color: #006699;\"><strong>date<\/strong><span style=\"color: black;\">&lt;<span style=\"color: blue;\">&#8216;2018-01-05&#8217;<span style=\"color: black;\">\u00a0<span style=\"color: #006699;\"><strong>then<\/strong><span style=\"color: black;\">\u00a0<span style=\"color: blue;\">&#8221;<span style=\"color: black;\">\u00a0<span style=\"color: #006699;\"><strong>else<\/strong><span style=\"color: black;\">\u00a0<span style=\"color: deeppink;\">sum<span style=\"color: black;\">(t.avg_cnt)\/<span style=\"color: deeppink;\">count<span style=\"color: black;\">(1)\u00a0<span style=\"color: #006699;\"><strong>end<\/strong><span style=\"color: black;\">\u00a0<span style=\"color: #006699;\"><strong>as<\/strong><span style=\"color: black;\">\u00a0avg_num\u00a0\u00a0<span style=\"color: #5c5c5c;\"><br \/>\n<\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/div>\n<\/li>\n<li>\n<div style=\"background: white;\"><span style=\"color: #006699; font-family: Consolas; font-size: 9pt;\"><strong>from<\/strong><span style=\"color: black;\">\u00a0\u00a0\u00a0<span style=\"color: #5c5c5c;\"><br \/>\n<\/span><\/span><\/span><\/div>\n<\/li>\n<li>\n<div style=\"background: #f8f8f8;\"><span style=\"color: black; font-family: Consolas; font-size: 9pt;\">(<span style=\"color: #006699;\"><strong>select<\/strong><span style=\"color: black;\">\u00a0b.<span style=\"color: #006699;\"><strong>date<\/strong><span style=\"color: black;\">,b.num,b.avg_5,a.num\u00a0<span style=\"color: #006699;\"><strong>as<\/strong><span style=\"color: black;\">\u00a0avg_cnt\u00a0<span style=\"color: #006699;\"><strong>from<\/strong><span style=\"color: black;\">\u00a0test_sales\u00a0<span style=\"color: #006699;\"><strong>as<\/strong><span style=\"color: black;\">\u00a0a\u00a0\u00a0\u00a0<span style=\"color: #5c5c5c;\"><br \/>\n<\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/div>\n<\/li>\n<li>\n<div style=\"background: white;\"><span style=\"color: deeppink; font-family: Consolas; font-size: 9pt;\">right<span style=\"color: black;\">\u00a0<span style=\"color: gray;\">join<span style=\"color: black;\">\u00a0test_sales\u00a0b\u00a0<span style=\"color: #006699;\"><strong>on<\/strong><span style=\"color: black;\">\u00a0b.<span style=\"color: #006699;\"><strong>date<\/strong><span style=\"color: black;\">\u00a0<span style=\"color: gray;\">between<span style=\"color: black;\">\u00a0a.<span style=\"color: #006699;\"><strong>date<\/strong><span style=\"color: black;\">\u00a0<span style=\"color: gray;\">and<span style=\"color: black;\">\u00a0date_add(a.<span style=\"color: #006699;\"><strong>date<\/strong><span style=\"color: black;\">,interval\u00a04\u00a0<span style=\"color: deeppink;\">DAY<span style=\"color: black;\">)\u00a0\u00a0<span style=\"color: #5c5c5c;\"><br \/>\n<\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/div>\n<\/li>\n<li>\n<div style=\"background: #f8f8f8;\"><span style=\"color: #006699; font-family: Consolas; font-size: 9pt;\"><strong>order<\/strong><span style=\"color: black;\">\u00a0<span style=\"color: #006699;\"><strong>by<\/strong><span style=\"color: black;\">\u00a0b.<span style=\"color: #006699;\"><strong>date<\/strong><span style=\"color: black;\">)\u00a0t\u00a0\u00a0<span style=\"color: #5c5c5c;\"><br \/>\n<\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/div>\n<\/li>\n<li>\n<div style=\"background: white;\"><span style=\"color: #006699; font-family: Consolas; font-size: 9pt;\"><strong>group<\/strong><span style=\"color: black;\">\u00a0<span style=\"color: #006699;\"><strong>by<\/strong><span style=\"color: black;\">\u00a0t.<span style=\"color: #006699;\"><strong>date<\/strong><span style=\"color: black;\">,t.num,t.avg_5\u00a0\u00a0<span style=\"color: #5c5c5c;\"><br \/>\n<\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/div>\n<\/li>\n<li>\n<div style=\"background: #f8f8f8;\"><span style=\"color: #006699; font-family: Consolas; font-size: 9pt;\"><strong>order<\/strong><span style=\"color: black;\">\u00a0<span style=\"color: #006699;\"><strong>by<\/strong><span style=\"color: black;\">\u00a0t.<span style=\"color: #006699;\"><strong>date<\/strong><span style=\"color: black;\">;\u00a0\u00a0<span style=\"color: #5c5c5c;\"><br \/>\n<\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/div>\n<\/li>\n<\/ol>\n<p>\u89e3\u91ca\u4e00\u4e0b\uff1a<\/p>\n<p>\u9996\u5148\uff0c\u770b\u7b2c4-6\u884c\uff0c\u6211\u4eec\u7528test_sales\u5173\u8054\u81ea\u8eab\uff0cjoin\u7684\u65f6\u5019\u4f7f\u7528right join on between\uff0c\u53ef\u4ee5\u7406\u89e3\u4e3a\u5c06a\u8868\u7684\u6bcf\u4e00\u6761\u6570\u636e\u90fd\u589e\u52a0\u4e864\u6761\u8bb0\u5f55\uff0c\u8fd9\u56db\u6761\u8bb0\u5f55\u7684\u65e5\u671f\u5206\u522b\u88ab\u52a0\u4e0a\u4e861\/2\/3\/4\u5929\u3002\u6b64\u65f6\uff0c\u518d\u4f7f\u7528right join\u65f6\uff0c\u4ece1\u67085\u65e5\u8d77\uff0c\u4e00\u6761b\u8868\u7684\u8bb0\u5f55\u90fd\u53ef\u4ee5\u5bf9\u5e94\u52305\u6761a\u8868\u76841\u67085\u65e5\u7684\u8bb0\u5f55\u3002<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/shanlin.info\/wp-content\/uploads\/2018\/07\/070518_1540_MySQL2.png\" alt=\"\" \/><\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u5bf9\u6bcf\u4e00\u4e2ab\u8868\u7684date\u800c\u8a00\uff0c\u6bcf\u5929\u90fd\u4f1a\u6709\u5bf9\u5e945\u5929\u5185\u7684avg_cnt\u6570\u636e\u3002\u56e0\u6b64\uff0c\u6309b.date\u5bf9avg_cnt\u6c42\u5747\u503c\u5373\u4e3a\u4e94\u65e5\u79fb\u52a8\u5e73\u5747\u503c\u3002<\/p>\n<p>\u8fd9\u662f\u7ed3\u679c\uff0cavg_num\u4e3a\u8ba1\u7b97\u51fa\u76845\u65e5\u79fb\u52a8\u5e73\u5747\u503c\uff0cavg_5\u662f\u539f\u59cb\u6570\u636e\uff0c\u4e8c\u8005\u7ed3\u679c\u4e00\u81f4\uff08\u7531\u4e8e\u4fdd\u7559\u5c0f\u6570\u4f4d\u6570\u95ee\u9898\u6709\u4e9b\u4e0d\u540c\uff09\u3002<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/shanlin.info\/wp-content\/uploads\/2018\/07\/070518_1540_MySQL3.png\" alt=\"\" \/><\/p>\n<p>\u5176\u4e2d\uff1a<span style=\"color: deeppink; font-size: 9pt;\"><span style=\"font-family: Consolas;\">case<span style=\"color: black;\">\u00a0<span style=\"color: #006699;\"><strong>when<\/strong><span style=\"color: black;\">\u00a0t.<span style=\"color: #006699;\"><strong>date<\/strong><span style=\"color: black;\">&lt;<span style=\"color: blue;\">&#8216;2018-01-05&#8217;<span style=\"color: black;\">\u00a0<span style=\"color: #006699;\"><strong>then<\/strong><span style=\"color: black;\">\u00a0<span style=\"color: blue;\">&#8221; <\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><span style=\"font-family: \u5b8b\u4f53;\">\u3002<\/span><\/span>\u662f\u4e3a\u4e86\u6392\u9664\u6ca1\u67095\u65e5\u5185\u79fb\u52a8\u5747\u503c\u7684\u6570\u636e\uff0c\u56e0\u4e3a\u6570\u636e\u4ece1\u67081\u65e5\u8d77\uff0c\u56e0\u6b64\u524d4\u4e2a\u65e5\u671f\u662f\u6ca1\u67095\u65e5\u79fb\u52a8\u5747\u503c\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u4eec\u6709\u8fd9\u6837\u7684\u4e00\u4e2a\u6570\u636e\u8868test_sales\uff0c\u8bb0\u5f55\u4e86\u6bcf\u65e5\u7684\u9500\u552e\u4ea7\u54c1\u7684\u9500\u552e\u6570\u91cf\u6570\u636e\u3002\u8868\u4e2ddate\u8868\u793a\u9500\u552e\u65e5\u671f\uff0cnu<\/p><\/div>\n<div class=\"blog-btn\"><a href=\"https:\/\/shanlin.info\/?p=156\" class=\"home-blog-btn\">\u9605\u8bfb\u66f4\u591a<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[26,29,28,27],"_links":{"self":[{"href":"https:\/\/shanlin.info\/index.php?rest_route=\/wp\/v2\/posts\/156"}],"collection":[{"href":"https:\/\/shanlin.info\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/shanlin.info\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/shanlin.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/shanlin.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=156"}],"version-history":[{"count":2,"href":"https:\/\/shanlin.info\/index.php?rest_route=\/wp\/v2\/posts\/156\/revisions"}],"predecessor-version":[{"id":158,"href":"https:\/\/shanlin.info\/index.php?rest_route=\/wp\/v2\/posts\/156\/revisions\/158"}],"wp:attachment":[{"href":"https:\/\/shanlin.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/shanlin.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=156"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/shanlin.info\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}