{"id":6957,"date":"2024-11-14T11:31:03","date_gmt":"2024-11-14T03:31:03","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6957\/"},"modified":"2024-11-14T11:31:03","modified_gmt":"2024-11-14T03:31:03","slug":"%e4%bd%bf%e7%94%a8group-by%e5%ad%90%e5%8f%a5%e6%97%b6%e6%8a%a5%e9%94%99%e7%9a%84%e5%8e%9f%e5%9b%a0%e5%92%8c%e8%a7%a3%e5%86%b3%e6%96%b9%e6%b3%95%e6%98%af%e4%bb%80%e4%b9%88%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6957\/","title":{"rendered":"\u4f7f\u7528GROUP BY\u5b50\u53e5\u65f6\u62a5\u9519\u7684\u539f\u56e0\u548c\u89e3\u51b3\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173018000119960.jpg\" class=\"aligncenter\" title=\"\u4f7f\u7528GROUP BY\u5b50\u53e5\u65f6\u62a5\u9519\u7684\u539f\u56e0\u548c\u89e3\u51b3\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f\u63d2\u56fe\" alt=\"\u4f7f\u7528GROUP BY\u5b50\u53e5\u65f6\u62a5\u9519\u7684\u539f\u56e0\u548c\u89e3\u51b3\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u4e2d\u4f7f\u7528group by\u65f6\u62a5\u9519<\/strong><\/p>\n<p>\u5728\u4f7f\u7528sql\u8bed\u53e5\u65f6\uff0c\u5982\u679c\u52a0\u4e0agroup by\u5b50\u53e5\uff0c\u5374\u51fa\u73b0\u4e86\u9519\u8bef\uff0c\u539f\u56e0\u53ef\u80fd\u5982\u4e0b\uff1a<\/p>\n<p><strong>\u672a\u6307\u5b9a<\/strong><\/p>\n<p>group by\u5b50\u53e5\u7528\u4e8e\u5c06\u7ed3\u679c\u96c6\u5206\u7ec4\uff0c\u4f46\u5982\u679c\u9488\u5bf9\u975e\u5206\u7ec4\u5b57\u6bb5\u4f7f\u7528\u4e86\u975e\u805a\u5408\u51fd\u6570\uff0c\u5c31\u4f1a\u5bfc\u81f4\u9519\u8bef\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u67e5\u8be2\u4f1a\u62a5\u9519\uff1a<\/p>\n<pre>select id, name from table group by name;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u89e3\u51b3\u529e\u6cd5\uff1a\u4e3a\u975e\u5206\u7ec4\u5b57\u6bb5\u6307\u5b9a\u805a\u5408\u51fd\u6570\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>select id, max(name) from table group by id;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u9ad8\u7248\u672c\u9650\u5236<\/strong><\/p>\n<p>\u5728mysql 5.6\u53ca\u66f4\u9ad8\u7248\u672c\u4e2d\uff0cgroup by\u5b50\u53e5\u5bf9\u975e\u5206\u7ec4\u5b57\u6bb5\u7684\u9650\u5236\u66f4\u52a0\u4e25\u683c\u3002\u5982\u679c\u975e\u5206\u7ec4\u5b57\u6bb5\u503c\u76f8\u540c\uff0c\u5219\u5fc5\u987b\u4f7f\u7528\u805a\u5408\u51fd\u6570\u5bf9\u5176\u8fdb\u884c\u5904\u7406\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u67e5\u8be2\u4f1a\u62a5\u9519\uff1a<\/p>\n<pre>select id, name from table group by id having name = 'john';<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u89e3\u51b3\u529e\u6cd5\uff1a\u5bf9\u975e\u5206\u7ec4\u5b57\u6bb5\u5e94\u7528\u805a\u5408\u51fd\u6570\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>SELECT id, MAX(name) FROM table GROUP BY id HAVING MAX(name) = 'John';<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4f7f\u7528GROUP BY\u5b50\u53e5\u65f6\u62a5\u9519\u7684\u539f\u56e0\u548c\u89e3\u51b3\u65b9\u6cd5\u662f\u4ec0\u4e48\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>\u4e2d\u4f7f\u7528group by\u65f6\u62a5\u9519 \u5728\u4f7f\u7528sql\u8bed\u53e5\u65f6\uff0c\u5982\u679c\u52a0\u4e0agroup by\u5b50\u53e5\uff0c\u5374\u51fa\u73b0\u4e86\u9519\u8bef\uff0c\u539f\u56e0\u53ef\u80fd\u5982\u4e0b\uff1a \u672a\u6307\u5b9a group by\u5b50\u53e5\u7528\u4e8e\u5c06\u7ed3\u679c\u96c6\u5206\u7ec4\uff0c\u4f46\u5982\u679c\u9488\u5bf9\u975e\u5206\u7ec4\u5b57\u6bb5\u4f7f\u7528\u4e86\u975e\u805a\u5408\u51fd\u6570\uff0c\u5c31\u4f1a\u5bfc\u81f4\u9519\u8bef\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u67e5\u8be2\u4f1a\u62a5\u9519\uff1a select id, name from table group by name; \u767b\u5f55\u540e\u590d\u5236 \u89e3\u51b3\u529e\u6cd5\uff1a\u4e3a\u975e\u5206\u7ec4\u5b57\u6bb5\u6307\u5b9a\u805a\u5408\u51fd\u6570\uff0c\u4f8b\u5982\uff1a select id, max(name) from table group by id; \u767b\u5f55\u540e\u590d\u5236 \u9ad8\u7248\u672c\u9650\u5236 \u5728mysql 5.6\u53ca\u66f4\u9ad8\u7248\u672c\u4e2d\uff0cgroup by\u5b50\u53e5\u5bf9\u975e\u5206\u7ec4\u5b57\u6bb5\u7684\u9650\u5236\u66f4\u52a0\u4e25\u683c\u3002\u5982\u679c\u975e\u5206\u7ec4\u5b57\u6bb5\u503c\u76f8\u540c\uff0c\u5219\u5fc5\u987b\u4f7f\u7528\u805a\u5408\u51fd\u6570\u5bf9\u5176\u8fdb\u884c\u5904\u7406\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u67e5\u8be2\u4f1a\u62a5\u9519\uff1a select id, name from table group by id having name = &#8216;john&#8217;; \u767b\u5f55\u540e\u590d\u5236 \u89e3\u51b3\u529e\u6cd5\uff1a\u5bf9\u975e\u5206\u7ec4\u5b57\u6bb5\u5e94\u7528\u805a\u5408\u51fd\u6570\uff0c\u4f8b\u5982\uff1a SELECT id, MAX(name) FROM table GROUP BY id HAVING MAX(name) = [&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-6957","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6957","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=6957"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6957\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6957"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6957"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6957"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}