{"id":27045,"date":"2024-11-24T14:28:12","date_gmt":"2024-11-24T06:28:12","guid":{"rendered":"https:\/\/fwq.ai\/blog\/27045\/"},"modified":"2024-11-24T14:28:12","modified_gmt":"2024-11-24T06:28:12","slug":"mysql-%e5%88%86%e7%bb%84%e6%9f%a5%e8%af%a2%e4%b8%ad-group-by-%e8%a6%81%e6%b1%82%ef%bc%9a%e4%bd%95%e6%97%b6%e9%9c%80%e8%a6%81%e7%a6%81%e7%94%a8-only_full_group_by-%e6%a8%a1%e5%bc%8f%ef%bc%9f-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/27045\/","title":{"rendered":"MySQL \u5206\u7ec4\u67e5\u8be2\u4e2d GROUP BY \u8981\u6c42\uff1a\u4f55\u65f6\u9700\u8981\u7981\u7528 ONLY_FULL_GROUP_BY \u6a21\u5f0f\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173115133720577.jpg\" class=\"aligncenter\" title=\"MySQL \u5206\u7ec4\u67e5\u8be2\u4e2d GROUP BY \u8981\u6c42\uff1a\u4f55\u65f6\u9700\u8981\u7981\u7528 ONLY_FULL_GROUP_BY \u6a21\u5f0f\uff1f\u63d2\u56fe\" alt=\"MySQL \u5206\u7ec4\u67e5\u8be2\u4e2d GROUP BY \u8981\u6c42\uff1a\u4f55\u65f6\u9700\u8981\u7981\u7528 ONLY_FULL_GROUP_BY \u6a21\u5f0f\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong> \u5206\u7ec4\u67e5\u8be2\u4e2d\u7684 group by \u8981\u6c42<\/strong><\/p>\n<p>\u5728 mysql \u4e2d\u6267\u884c\u5206\u7ec4\u67e5\u8be2\u65f6\uff0c\u901a\u5e38\u9700\u8981\u5728 group by \u5b50\u53e5\u4e2d\u6307\u5b9a select \u8bed\u53e5\u4e2d\u6d89\u53ca\u7684\u6240\u6709\u975e\u805a\u5408\u5217\u3002\u7136\u800c\uff0c\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u60a8\u53ef\u80fd\u4f1a\u53d1\u73b0\u8fd9\u4e00\u8981\u6c42\u5e76\u4e0d\u4e25\u683c\u3002<\/p>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u4ece mysql 5.7 \u5f00\u59cb\uff0conly_full_group_by \u6a21\u5f0f\u5df2\u542f\u7528\u3002\u5728\u8fd9\u79cd\u6a21\u5f0f\u4e0b\uff0c\u6240\u6709\u975e\u805a\u5408\u5217\u90fd\u5fc5\u987b\u51fa\u73b0\u5728 group by \u5b50\u53e5\u4e2d\u3002\u8fd9\u6709\u52a9\u4e8e\u786e\u4fdd\u7ed3\u679c\u7684\u6b63\u786e\u6027\u548c\u4e00\u81f4\u6027\u3002<\/p>\n<p>\u4f46\u662f\uff0c\u60a8\u53ef\u4ee5\u9009\u62e9\u7981\u7528 only_full_group_by \u6a21\u5f0f\u3002\u4e3a\u6b64\uff0c\u8bf7\u5c06 only_full_group_by \u9009\u9879\u4ece sql_mode \u8bbe\u7f6e\u4e2d\u5220\u9664\u3002\u7981\u7528\u540e\uff0cmysql \u5c06\u4e0d\u518d\u5f3a\u5236\u6267\u884c group by \u4e2d\u7684\u5b8c\u6574\u5b57\u6bb5\u5217\u8868\u3002<\/p>\n<p>\u4f8b1\uff1a\u4f7f\u7528 only_full_group_by \u6a21\u5f0f<\/p>\n<pre>select * from table_name group by column1;\n-- \u4f1a\u629b\u51fa\u9519\u8bef\uff0c\u56e0\u4e3a column2 \u672a\u5305\u542b\u5728 group by \u5b50\u53e5\u4e2d<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4f8b2\uff1a\u7981\u7528 only_full_group_by \u6a21\u5f0f<\/p>\n<pre>SET SQL_MODE = 'ALLOW_PARTIAL_GROUP_BY';\n\nSELECT * FROM table_name GROUP BY column1, column3;\n-- \u4e0d\u4f1a\u629b\u51fa\u9519\u8bef\uff0cgroup by \u5141\u8bb8\u90e8\u5206\u5b57\u6bb5<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u7981\u7528 only_full_group_by \u6a21\u5f0f\u53ef\u80fd\u4f1a\u5bf9\u67e5\u8be2\u7ed3\u679c\u7684\u51c6\u786e\u6027\u4ea7\u751f\u8d1f\u9762\u5f71\u54cd\u3002 dlatego disarankan untuk menggunakan modenya hanya jika benar-benar diperlukan.<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fMySQL \u5206\u7ec4\u67e5\u8be2\u4e2d GROUP BY \u8981\u6c42\uff1a\u4f55\u65f6\u9700\u8981\u7981\u7528 ONLY_FULL_GROUP_BY \u6a21\u5f0f\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5206\u7ec4\u67e5\u8be2\u4e2d\u7684 group by \u8981\u6c42 \u5728 mysql \u4e2d\u6267\u884c\u5206\u7ec4\u67e5\u8be2\u65f6\uff0c\u901a\u5e38\u9700\u8981\u5728 group by \u5b50\u53e5\u4e2d\u6307\u5b9a select \u8bed\u53e5\u4e2d\u6d89\u53ca\u7684\u6240\u6709\u975e\u805a\u5408\u5217\u3002\u7136\u800c\uff0c\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u60a8\u53ef\u80fd\u4f1a\u53d1\u73b0\u8fd9\u4e00\u8981\u6c42\u5e76\u4e0d\u4e25\u683c\u3002 \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u4ece mysql 5.7 \u5f00\u59cb\uff0conly_full_group_by \u6a21\u5f0f\u5df2\u542f\u7528\u3002\u5728\u8fd9\u79cd\u6a21\u5f0f\u4e0b\uff0c\u6240\u6709\u975e\u805a\u5408\u5217\u90fd\u5fc5\u987b\u51fa\u73b0\u5728 group by \u5b50\u53e5\u4e2d\u3002\u8fd9\u6709\u52a9\u4e8e\u786e\u4fdd\u7ed3\u679c\u7684\u6b63\u786e\u6027\u548c\u4e00\u81f4\u6027\u3002 \u4f46\u662f\uff0c\u60a8\u53ef\u4ee5\u9009\u62e9\u7981\u7528 only_full_group_by \u6a21\u5f0f\u3002\u4e3a\u6b64\uff0c\u8bf7\u5c06 only_full_group_by \u9009\u9879\u4ece sql_mode \u8bbe\u7f6e\u4e2d\u5220\u9664\u3002\u7981\u7528\u540e\uff0cmysql \u5c06\u4e0d\u518d\u5f3a\u5236\u6267\u884c group by \u4e2d\u7684\u5b8c\u6574\u5b57\u6bb5\u5217\u8868\u3002 \u4f8b1\uff1a\u4f7f\u7528 only_full_group_by \u6a21\u5f0f select * from table_name group by column1; &#8212; \u4f1a\u629b\u51fa\u9519\u8bef\uff0c\u56e0\u4e3a column2 \u672a\u5305\u542b\u5728 group by \u5b50\u53e5\u4e2d \u767b\u5f55\u540e\u590d\u5236 \u4f8b2\uff1a\u7981\u7528 only_full_group_by \u6a21\u5f0f SET SQL_MODE = [&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-27045","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/27045","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=27045"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/27045\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=27045"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=27045"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=27045"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}