{"id":230113,"date":"2020-12-20T16:21:36","date_gmt":"2020-12-20T08:21:36","guid":{"rendered":"http:\/\/4563.org\/?p=230113"},"modified":"2020-12-20T16:21:36","modified_gmt":"2020-12-20T08:21:36","slug":"mysql-%e8%af%ad%e5%8f%a5%e6%b1%82%e6%8c%87%e7%82%b9","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=230113","title":{"rendered":"MySQL \u8bed\u53e5\u6c42\u6307\u70b9"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  MySQL \u8bed\u53e5\u6c42\u6307\u70b9               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : sytnishizuiai <\/span>  <span><i><\/i> 5<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\">                         &lt;thead&gt; &lt;\/thead&gt; <\/p>\n<table>\n<tbody>\n<tr>\n<th>id<\/th>\n<th>fid<\/th>\n<th>date<\/th>\n<\/tr>\n<\/tbody>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>a<\/td>\n<td>2020-12-10<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>a<\/td>\n<td>2020-12-30<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>b<\/td>\n<td>2020-12-15<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6c42\u6559\u4e0b\uff0c\u6211\u5199\u591a\u8868\u8054\u67e5\u7684\u65f6\u5019\uff0c\u5b50\u8868\u60f3\u8981 date \u5c0f\u4e8e\u5f53\u524d\u65e5\u671f\uff0cfid \u76f8\u540c\u7684\u90fd\u4e0d\u8981\uff08 date&lt;2020-12-25\uff0c \u6240\u6709 fid=a \u90fd\u4e0d\u8981\uff09\uff0c\u8fd9\u79cd\u600e\u4e48\u5199\uff0c\u60f3\u4e86\u597d\u4e45\u5199\u4e0d\u51fa\u4e5f\u67e5\u4e0d\u5230\u3002 \u5b9e\u5728\u4e0d\u884c\u6211\u5c31\u5165\u5e93\u7684\u65f6\u5019\u7b97\u51fa\u6700\u5927\u65e5\u671f\u5b58\u5165\u4e3b\u8868\u4e86\u3002\uff08\u800c\u4e14\u591a\u8868\u67e5\u8be2\u8fd8\u8981\u5206\u9875\uff09<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>9<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"4719132\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : l00t <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5148\u628a\u9700\u6c42\u60f3\u6e05\u695a\u8bf4\u660e\u767d\u2026\u2026 \u591a\u8868\u8054\u67e5\uff0c\u8868\u5462\uff1f\u8981\u67e5\u4ec0\u4e48\uff1f\u5173\u8054\u6761\u4ef6\u662f\u4ec0\u4e48\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4719133\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : sytnishizuiai <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @l00t \u62b1\u6b49\uff0c\u56e0\u4e3a\u8868\u6bd4\u8f83\u591a\uff0c\u60f3\u5148\u95ee\u95ee\u5982\u679c\u5355\u8868\u53d1\u751f\u8fd9\u79cd\u60c5\u51b5\u8be5\u600e\u4e48\u529e\u3002\u521a\u624d\u7528 MAX \u53ef\u4ee5\u4e86\uff0c\u4f46\u662f\u591a\u8868\u67e5\u8d8a\u5199\u8d8a\u590d\u6742\uff0c\u4e4b\u524d\u5c31\u5df2\u7ecf 3 \u8868\u67e5\u4e86\uff0c\u73b0\u5728\u8981 4 \u8868\u4e86\u3002<\/p>\n<p>\u6240\u4ee5\u51c6\u5907\u62c6\u5f00\u67e5\uff0c\u53cd\u6b63\u6027\u80fd\u6ca1\u4ec0\u4e48\u8981\u6c42\uff0c\u540e\u53f0\u7528\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4719134\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : berg223 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7b2c\u4e00\u6b65\uff1aselect count(*),fid from &lt;table&gt; where date &lt; 2020-12-25 group by fid<br \/>\u7b2c\u4e8c\u6b65\uff1a\u7ed3\u679c\u96c6\u4e2d\u8fc7\u6ee4\u6389 count(*) &gt; 1 \u7684\u884c\u5373\u53ef\u3002\u5b9e\u73b0\u8fd9\u4e00\u6b65\u6709\u4e24\u79cd\u9009\u62e9\u65b9\u5f0f\uff1a1.\u5728\u4e1a\u52a1\u4ee3\u7801\u4e2d\u5b9e\u73b0\uff1b 2.\u7ee7\u7eed\u7528 sql \u5b9e\u73b0\uff0c\u5199\u5b8c\u4e0d\u8981\u5fd8\u8bb0\u7528 explain \u770b\u4e00\u4e0b\u53ef\u4e0d\u53ef\u4ee5\u8d70\u7d22\u5f15\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4719135\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : sytnishizuiai <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @berg223 \u55ef\uff0c\u548c\u6211\u7528 max \u627e\u51fa\u6bcf\u7ec4 fid \u6700\u5927\u7684 date \u6548\u679c\u4e00\u6837\uff0c\u4e0d\u8fc7\u4f1a\u9020\u6210 4 \u8868\u67e5\u8be2\uff0c\u51c6\u5907\u4e0d\u8d70\u8fd9\u6761\u8def\u4e86\uff0c\u8981\u4e48\u5165\u5e93\u7684\u65f6\u5019\u628a\u6700\u5927\u7684 date \u5b58\u5165\u4e3b\u8868\uff0c\u8fd9\u6837\u7b80\u5316\u67e5\u8be2\uff0c\u8981\u4e48\u5c31\u91cd\u65b0\u5199\u8fd9\u5757\u7684\u591a\u8868\u67e5\u8be2\u3002<br \/>\u8c22\u8c22\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4719136\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : hanbin <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             EXPLAIN SELECT <br \/> MASTER.id <br \/>FROM<br \/> MASTER jion other_a AS a ON MASTER.id = a.master_id <br \/>WHERE<br \/> a.date &lt; 2020-12-25 <br \/> AND a.fid &lt;&gt; &#8216;a&#8217;<\/p>\n<p>\u8054\u8868\u67e5\u8be2\u3001\u8054\u5408\u67e5\u8be2\u3001\u4e34\u65f6\u8868\u3001\u6761\u4ef6\u8bed\u53e5\u7ec4\u5408\u4f7f\u7528\u5373\u53ef\u3002\u6ca1\u6709\u7528 sql \u89e3\u51b3\u4e0d\u4e86\u7684\u9700\u6c42\uff0c\u5982\u679c\u6709\uff0c\u90a3\u5c31\u662f\u5b66\u4f1a\u7684\u5947\u6deb\u6280\u5de7\u4e0d\u591f\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4719137\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : keepeye <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @hanbin \u4ed6\u8981\u7684\u662f\u8fc7\u6ee4 fid \u51fa\u73b0\u8d85\u8fc7\u4e00\u6b21\u7684\uff0c\u4e0d\u662f\u7279\u6307 &#8216;a&#8217;                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4719138\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : keepeye <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5b50\u67e5\u8be2\u4e0d\u80fd\u7528 group by \u5417\uff1f<br \/>select a.*, b.* from a inner join (select fid, count(*) as c from b where `date` &lt; &#8216;\u5f53\u524d\u65e5\u671f&#8217; group by fid having c = 1) as b WHERE \u5176\u4ed6\u6761\u4ef6..                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4719139\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : sytnishizuiai <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @keepeye \u55ef\uff0c\u6211\u521a\u624d\u6d4b\u8bd5 max group by \u628a\u6570\u636e\u7b5b\u9009\u540e\u518d\u8054\u7ed3\u67e5\uff0c\u4e0d\u8fc7\u5c31 4 \u4e2a\u8868\u8054\u67e5\u4e86\uff0c\u6211\u51c6\u5907\u8fd9\u4e2a\u8868\u5355\u72ec\u5199\u4e2a\u67e5\uff0c\u7136\u540e\u6570\u636e\u5408\u5e76\u7b5b\u9009\uff0c\u8fd9\u6837\u65b9\u4fbf\u70b9\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4719140\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : sytnishizuiai <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8fd9\u529f\u80fd\u521a\u624d\u8ba8\u8bba\u540e\u4e0d\u505a\u4e86\u3002\u3002\u3002\u592a\u68d2\u4e86\u3002\u3002\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>MySQL \u8bed\u53e5\u6c42\u6307\u70b9 \u8cc7\u6df1\u5927\u4f6c &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\/230113"}],"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=230113"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/230113\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=230113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=230113"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=230113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}