{"id":6992,"date":"2024-11-14T11:04:43","date_gmt":"2024-11-14T03:04:43","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6992\/"},"modified":"2024-11-14T11:04:43","modified_gmt":"2024-11-14T03:04:43","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-mysql-%e6%9f%a5%e8%af%a2%e6%96%87%e7%ab%a0%e5%8f%8a%e5%85%b6%e6%9c%80%e6%96%b0%e7%9a%84-5-%e6%9d%a1%e8%af%84%e8%ae%ba%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6992\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 MySQL \u67e5\u8be2\u6587\u7ae0\u53ca\u5176\u6700\u65b0\u7684 5 \u6761\u8bc4\u8bba\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173025498064062.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528 MySQL \u67e5\u8be2\u6587\u7ae0\u53ca\u5176\u6700\u65b0\u7684 5 \u6761\u8bc4\u8bba\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528 MySQL \u67e5\u8be2\u6587\u7ae0\u53ca\u5176\u6700\u65b0\u7684 5 \u6761\u8bc4\u8bba\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>java  \u67e5\u8be2\u6587\u7ae0\u53ca\u5176\u6700\u8fd1 5 \u6761\u8bc4\u8bba<\/strong><\/p>\n<p>\u9700\u6c42\u662f\u67e5\u8be2\u6587\u7ae0\u5217\u8868\uff0c\u6bcf\u4e2a\u6587\u7ae0\u5c55\u793a\u5176\u6700\u8fd1 5 \u6761\u8bc4\u8bba\u3002\u6570\u636e\u5b58\u50a8\u5728\u4e24\u4e2a\u8868\u4e2d\uff1a<\/p>\n<pre>article \u8868\uff1a\nid, content\n\ncomment \u8868\uff1a\nid, pid, comment<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528 left join \u67e5\u8be2\u5b9e\u73b0\uff0c\u4f46\u5b83\u4f1a\u8fd4\u56de\u6240\u6709\u8bc4\u8bba\u3002\u4e3a\u4e86\u9650\u5236\u8bc4\u8bba\u6570\u91cf\u4e3a 5 \u6761\uff0c\u9700\u8981\u4e00\u4e2a\u5b50\u67e5\u8be2\u6765\u7b5b\u9009\u53ea\u5305\u542b\u7279\u5b9a\u6570\u91cf\u8bc4\u8bba\u7684\u6587\u7ae0\u3002<\/p>\n<p><strong>sql \u67e5\u8be2<\/strong><\/p>\n<pre>select \n    tmp1.id, tmp1.content, tmp.comment\nfrom\n    (select\n    a.pid, a.comment\n     from\n         `comment` a\n     where\n         5 &gt; (select count(id) from `comment` b where b.pid = a.pid and a.id &gt; b.id)\n     order by\n         a.id desc) tmp\n     join article tmp1 on tmp.pid = tmp1.id<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6838\u5fc3\u5b50\u67e5\u8be2<\/strong><\/p>\n<pre>3 &gt; (select count(id) from `comment` b where b.pid = a.pid and a.id &gt; b.id)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6b64\u5b50\u67e5\u8be2\u68c0\u67e5\u662f\u5426\u5b58\u5728\u591a\u4e8e 5 \u6761\u8f83\u65b0\u8bc4\u8bba\u3002\u5982\u679c\u5b58\u5728\uff0c\u5219\u4ece\u7ed3\u679c\u4e2d\u6392\u9664\u8be5\u8bc4\u8bba\u3002\u5b83\u901a\u8fc7\u68c0\u67e5\u5f53\u524d\u8bc4\u8bba\u7684 id \u662f\u5426\u5927\u4e8e\u4efb\u4f55\u540e\u7eed\u8bc4\u8bba\u7684 id \u6765\u5b9e\u73b0\u3002<\/p>\n<p><strong>\u6309\u6700\u65b0\u8bc4\u8bba\u6392\u5e8f<\/strong><\/p>\n<p>order by \u5b50\u53e5\u786e\u4fdd\u8bc4\u8bba\u6309\u4ece\u6700\u65b0\u5230\u6700\u65e7\u7684\u987a\u5e8f\u6392\u5e8f\u3002<\/p>\n<p><strong>\u7ed3\u679c<\/strong><\/p>\n<p>\u6267\u884c\u6b64\u67e5\u8be2\u5c06\u8fd4\u56de\u5177\u6709\u9650\u5236\u8bc4\u8bba\u6570\u91cf\uff085 \u6761\uff09\u7684\u6587\u7ae0\u5217\u8868\u3002<\/p>\n<p><strong>\u6700\u65b0 3 \u6761\u8bc4\u8bba\u7684\u53d8\u4f53<\/strong><\/p>\n<p>\u82e5\u8981\u67e5\u8be2\u6700\u65b0\u7684 3 \u6761\u8bc4\u8bba\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u53d8\u4f53\uff1a<\/p>\n<pre>WHERE\n         3 &gt; (SELECT COUNT(id) FROM `comment` b WHERE b.pid = a.pid AND a.id &lt; b.id)\nORDER BY\n         a.id<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6b64\u53d8\u4f53\u901a\u8fc7\u68c0\u67e5\u5f53\u524d\u8bc4\u8bba\u7684 id \u662f\u5426\u5c0f\u4e8e\u4efb\u4f55\u8f83\u65e7\u8bc4\u8bba\u7684 id \u6765\u7b5b\u9009\u8bc4\u8bba\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528 MySQL \u67e5\u8be2\u6587\u7ae0\u53ca\u5176\u6700\u65b0\u7684 5 \u6761\u8bc4\u8bba\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>java \u67e5\u8be2\u6587\u7ae0\u53ca\u5176\u6700\u8fd1 5 \u6761\u8bc4\u8bba \u9700\u6c42\u662f\u67e5\u8be2\u6587\u7ae0\u5217\u8868\uff0c\u6bcf\u4e2a\u6587\u7ae0\u5c55\u793a\u5176\u6700\u8fd1 5 \u6761\u8bc4\u8bba\u3002\u6570\u636e\u5b58\u50a8\u5728\u4e24\u4e2a\u8868\u4e2d\uff1a article \u8868\uff1a id, content comment \u8868\uff1a id, pid, comment \u767b\u5f55\u540e\u590d\u5236 \u53ef\u4ee5\u4f7f\u7528 left join \u67e5\u8be2\u5b9e\u73b0\uff0c\u4f46\u5b83\u4f1a\u8fd4\u56de\u6240\u6709\u8bc4\u8bba\u3002\u4e3a\u4e86\u9650\u5236\u8bc4\u8bba\u6570\u91cf\u4e3a 5 \u6761\uff0c\u9700\u8981\u4e00\u4e2a\u5b50\u67e5\u8be2\u6765\u7b5b\u9009\u53ea\u5305\u542b\u7279\u5b9a\u6570\u91cf\u8bc4\u8bba\u7684\u6587\u7ae0\u3002 sql \u67e5\u8be2 select tmp1.id, tmp1.content, tmp.comment from (select a.pid, a.comment from `comment` a where 5 &gt; (select count(id) from `comment` b where b.pid = a.pid and a.id &gt; b.id) order by a.id [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-6992","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6992","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=6992"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6992\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6992"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6992"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6992"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}