{"id":201632,"date":"2020-11-25T05:55:19","date_gmt":"2020-11-24T21:55:19","guid":{"rendered":"http:\/\/4563.org\/?p=201632"},"modified":"2020-11-25T05:55:19","modified_gmt":"2020-11-24T21:55:19","slug":"mysql-5-7-%e4%b8%a4%e4%b8%aa%e4%b8%b4%e6%97%b6%e8%a1%a8-%e5%b7%a6%e5%8f%b3%e8%bf%9e%e6%8e%a5%ef%bc%8c%e5%90%8c%e6%a0%b7%e7%9a%84%e4%b8%b4%e6%97%b6%e8%a1%a8-join-%e8%bf%9e%e6%8e%a5%e4%b8%8d%e4%b8%8a","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=201632","title":{"rendered":"mysql 5.7 \u4e24\u4e2a\u4e34\u65f6\u8868 \u5de6\u53f3\u8fde\u63a5\uff0c\u540c\u6837\u7684\u4e34\u65f6\u8868 join \u8fde\u63a5\u4e0d\u4e0a"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  mysql 5.7 \u4e24\u4e2a\u4e34\u65f6\u8868 \u5de6\u53f3\u8fde\u63a5\uff0c\u540c\u6837\u7684\u4e34\u65f6\u8868 join \u8fde\u63a5\u4e0d\u4e0a               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : GroupF <\/span>  <span><i><\/i> 4<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<h1>\u76f4\u63a5\u80fd\u8dd1\u7684 sql \u5949\u4e0a \uff0c\u53ef\u80fd\u547d\u540d\u4e0d\u592a\u89c4\u8303<\/h1>\n<p> <\/p>\n<h1>\u5de6\u8868\u4e3a\u4e3b\u8868\u5c31\u51fa\u5de6\u8868\uff0c\u53f3\u8868\u4e3a\u4e3b\u8868\u51fa\u53f3\u8868<\/h1>\n<p> <\/p>\n<p>select * from<\/p>\n<p>(select DATE_FORMAT(m1, &#8216;%Y-%m&#8217;) as day3 from ( select (&#8216;2020-01-23 00:00:00&#8217; &#8211; INTERVAL DAYOFMONTH(&#8216;2020-01-23 00:00:00&#8242;)-1 DAY) +INTERVAL m MONTH as m1 from ( select @rownum:[email&#160;protected]+1 as m from (select 1 union select 2 union select 3 union select 4) t1, (select 1 union select 2 union select 3 union select 4) t2, (select 1 union select 2 union select 3 union select 4) t3, (select @rownum:=-1) t0 ) d1 ) d2 where m1&lt;=&#8217;2020-12-01 00:00:00&#8217; order by m1) as datemonths3<\/p>\n<p>left join (select DATE_FORMAT(m1, &#8216;%Y-%m&#8217;) as day1 from ( select (&#8216;2020-01-23 00:00:00&#8217; &#8211; INTERVAL DAYOFMONTH(&#8216;2020-01-23 00:00:00&#8242;)-1 DAY) +INTERVAL m MONTH as m1 from ( select @rownum:[email&#160;protected]+1 as m from (select 1 union select 2 union select 3 union select 4) t1, (select 1 union select 2 union select 3 union select 4) t2, (select 1 union select 2 union select 3 union select 4) t3, (select @rownum:=-1) t0 ) d1 ) d2 where m1&lt;=&#8217;2020-12-01 00:00:00&#8217; order by m1) as datemonths2 on datemonths2.day1 = datemonths3.day3<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>4<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"4214382\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : mm163 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             union                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4214383\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : GroupF <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @mm163 \u4f46\u662f union\uff0c\u5b8c\u6210\u4e0d\u4e86 join \u7684\u4e8b\u60c5\uff0c\u6211\u56fa\u5b9a\u6761\u6570\uff0c\u6ca1\u6709\u8865 0                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4214384\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : GroupF <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u518d\u52a0\u4e0a\u6761\u4ef6\u5e94\u8be5\u80fd\u884c\uff0c\u90a3\u6211\u5c31\u662f\u5728\u8868\u8fde\u63a5\u4ee5\u53ca\u7528\u51fd\u6570\u7684\u65f6\u5019\u4ed6\u4eec\u8fde\u63a5\uff0c\u6210\u4e3a\u65b0\u8868\u65f6\u5019\u7684\u987a\u5e8f\u662f\u600e\u4e48<br \/>\u4e3e\u4e2a\u5c0f\u4f8b\u5b50<br \/>select * from<br \/>((select &#8216;2020&#8217; as b,&#8217;20211&#8242; as a ) union<br \/>(select &#8216;2021&#8217; as b,&#8217;20211&#8242; ) ) as av<br \/> group by av.a                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4214385\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : GroupF <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4ec5\u9488\u5bf9\u8fd9\u4e2a\u4f8b\u5b50\uff0c\u6211\u7528 having \u8fd8\u662f\u80fd\u6ee1\u8db3\u5927\u6982<br \/>#\u5206\u7ec4\uff0ca \u53ea\u80fd\u51fa\u73b0\u4e00\u6b21\uff0c\u4f46\u662f\u53ef\u80fd b \u6ca1\u6570\u636e\uff0c\u9009\u6709\u6570\u636e\u7684\u5206<br \/>select * from<br \/>((select &#8216;2020&#8217; as b,&#8217;20221&#8242; as a ) union<br \/>(select &#8216;2021&#8217; as b,&#8217;20221&#8242; ) ) as av<br \/> group by av.a having (b &gt; 0 or b=0)                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>mysql 5.7 \u4e24\u4e2a\u4e34\u65f6\u8868 \u5de6&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/201632"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=201632"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/201632\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=201632"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=201632"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=201632"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}