{"id":624,"date":"2025-01-01T10:33:06","date_gmt":"2025-01-01T02:33:06","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=624"},"modified":"2025-01-01T10:33:06","modified_gmt":"2025-01-01T02:33:06","slug":"%e5%9c%a8-mysql-%e4%b8%ad%ef%bc%8c%e6%97%a0%e6%b3%95%e8%bf%9b%e8%a1%8c%e5%88%86%e7%bb%84%ef%bc%88group-by%ef%bc%89%ef%bc%8c%e7%bb%8f%e5%b8%b8%e6%8a%a5%e9%94%99%e6%98%af%e4%bb%80%e4%b9%88%e5%8e%9f","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/01\/01\/%e5%9c%a8-mysql-%e4%b8%ad%ef%bc%8c%e6%97%a0%e6%b3%95%e8%bf%9b%e8%a1%8c%e5%88%86%e7%bb%84%ef%bc%88group-by%ef%bc%89%ef%bc%8c%e7%bb%8f%e5%b8%b8%e6%8a%a5%e9%94%99%e6%98%af%e4%bb%80%e4%b9%88%e5%8e%9f\/","title":{"rendered":"\u5728 MySQL \u4e2d\uff0c\u65e0\u6cd5\u8fdb\u884c\u5206\u7ec4\uff08GROUP BY\uff09\uff0c\u7ecf\u5e38\u62a5\u9519\u662f\u4ec0\u4e48\u539f\u56e0\uff1f"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u5728 MySQL \u4e2d\uff0c\u65e0\u6cd5\u8fdb\u884c\u5206\u7ec4\uff08<code>GROUP BY<\/code>\uff09\u7684\u9519\u8bef\u901a\u5e38\u4e0e\u67e5\u8be2\u4e2d\u7684\u9009\u62e9\u5b57\u6bb5\u3001\u805a\u5408\u51fd\u6570\uff08\u5982 <code>COUNT()<\/code>\u3001<code>SUM()<\/code>\u3001<code>AVG()<\/code> \u7b49\uff09\u4f7f\u7528\u4e0d\u5f53\uff0c\u6216\u8005\u5206\u7ec4\u5b57\u6bb5\u4e0e\u9009\u62e9\u5b57\u6bb5\u4e4b\u95f4\u7684\u5173\u7cfb\u4e0d\u7b26\u5408 SQL \u6807\u51c6\u6709\u5173\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u95ee\u9898\u53ca\u5176\u89e3\u51b3\u65b9\u6cd5\uff1a<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u5206\u7ec4\u65f6\u9009\u62e9\u5b57\u6bb5\u6ca1\u6709\u805a\u5408\u51fd\u6570<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u5728\u4f7f\u7528 <code>GROUP BY<\/code> \u65f6\uff0c\u4f60\u7684 <code>SELECT<\/code> \u8bed\u53e5\u4e2d\u5305\u542b\u4e86\u4e0d\u5728 <code>GROUP BY<\/code> \u4e2d\u7684\u5217\uff0c\u800c\u8fd9\u4e9b\u5217\u6ca1\u6709\u5e94\u7528\u805a\u5408\u51fd\u6570\uff08\u5982 <code>COUNT()<\/code>\u3001<code>SUM()<\/code>\u3001<code>AVG()<\/code> \u7b49\uff09\uff0cMySQL \u4f1a\u62a5\u9519\uff0c\u56e0\u4e3a\u5b83\u4e0d\u77e5\u9053\u8be5\u5982\u4f55\u5904\u7406\u8fd9\u4e9b\u5217\u7684\u503c\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f8b\u5b50\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT name, age, COUNT(*) FROM users GROUP BY age;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u67e5\u8be2\u4f1a\u5bfc\u81f4\u9519\u8bef\uff0c\u56e0\u4e3a <code>name<\/code> \u5217\u6ca1\u6709\u4f7f\u7528\u805a\u5408\u51fd\u6570\uff0c\u800c\u5b83\u53c8\u6ca1\u6709\u51fa\u73b0\u5728 <code>GROUP BY<\/code> \u5b50\u53e5\u4e2d\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u89e3\u51b3\u65b9\u6cd5\uff1a<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u8981\u4e48\u5c06\u6240\u6709\u975e\u5206\u7ec4\u5217\u90fd\u653e\u5165 <code>GROUP BY<\/code> \u5b50\u53e5\u4e2d\uff0c\u8981\u4e48\u5bf9\u5b83\u4eec\u5e94\u7528\u9002\u5f53\u7684\u805a\u5408\u51fd\u6570\u3002\u4ee5\u4e0b\u662f\u4e24\u79cd\u53ef\u80fd\u7684\u4fee\u590d\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4fee\u590d 1<\/strong>\uff1a\u5c06\u6240\u6709\u9009\u62e9\u7684\u5b57\u6bb5\u653e\u5165 <code>GROUP BY<\/code> \u4e2d\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT name, age, COUNT(*) FROM users GROUP BY name, age;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4fee\u590d 2<\/strong>\uff1a\u5bf9\u975e\u5206\u7ec4\u5b57\u6bb5\u4f7f\u7528\u805a\u5408\u51fd\u6570\uff08\u4f8b\u5982 <code>MAX()<\/code>\u3001<code>MIN()<\/code>\u3001<code>GROUP_CONCAT()<\/code> \u7b49\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT MAX(name), age, COUNT(*) FROM users GROUP BY age;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>\u4f7f\u7528 <code>GROUP BY<\/code> \u65f6\u7684\u591a\u4f59\u5b57\u6bb5<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">MySQL \u5728\u6267\u884c\u5206\u7ec4\u65f6\uff0c\u4f1a\u6309\u7167 <code>GROUP BY<\/code> \u5b50\u53e5\u6307\u5b9a\u7684\u5b57\u6bb5\u8fdb\u884c\u5206\u7ec4\u3002\u5982\u679c\u9009\u62e9\u7684\u5b57\u6bb5\u6ca1\u6709\u51fa\u73b0\u5728 <code>GROUP BY<\/code> \u4e2d\uff0c\u4e14\u6ca1\u6709\u9002\u5f53\u7684\u805a\u5408\u51fd\u6570\uff0c\u5b83\u4f1a\u5f15\u53d1\u9519\u8bef\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f8b\u5b50\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT department, name FROM employees GROUP BY department;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c <code>name<\/code> \u5b57\u6bb5\u6ca1\u6709\u4f7f\u7528\u805a\u5408\u51fd\u6570\u4e14\u4e0d\u5728 <code>GROUP BY<\/code> \u5b50\u53e5\u4e2d\uff0cMySQL \u4f1a\u629b\u51fa\u9519\u8bef\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u89e3\u51b3\u65b9\u6cd5\uff1a<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528\u805a\u5408\u51fd\u6570\u6765\u5904\u7406 <code>name<\/code> \u5b57\u6bb5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT department, GROUP_CONCAT(name) FROM employees GROUP BY department;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>GROUP_CONCAT()<\/code> \u4f1a\u5c06\u6bcf\u4e2a\u7ec4\u4e2d\u7684 <code>name<\/code> \u503c\u5408\u5e76\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u89e3\u51b3\u4e86\u5206\u7ec4\u95ee\u9898\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>\u4f7f\u7528 <code>HAVING<\/code> \u800c\u4e0d\u662f <code>WHERE<\/code><\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>HAVING<\/code> \u7528\u4e8e\u5bf9 <code>GROUP BY<\/code> \u540e\u7684\u7ed3\u679c\u8fdb\u884c\u7b5b\u9009\uff0c\u800c <code>WHERE<\/code> \u7528\u4e8e\u5bf9\u539f\u59cb\u6570\u636e\u8fdb\u884c\u7b5b\u9009\u3002\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u9519\u8bef\u5730\u5c06\u7b5b\u9009\u6761\u4ef6\u653e\u5165 <code>WHERE<\/code> \u4e2d\uff0c\u800c\u5e94\u653e\u5165 <code>HAVING<\/code> \u4e2d\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u65e0\u6cd5\u6b63\u5e38\u5206\u7ec4\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f8b\u5b50\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT department, COUNT(*) FROM employees WHERE COUNT(*) &gt; 5 GROUP BY department;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u67e5\u8be2\u662f\u9519\u8bef\u7684\uff0c\u56e0\u4e3a <code>COUNT(*)<\/code> \u662f\u5728 <code>GROUP BY<\/code> \u4e4b\u540e\u8fdb\u884c\u8ba1\u7b97\u7684\uff0c\u6240\u4ee5\u4e0d\u80fd\u5728 <code>WHERE<\/code> \u5b50\u53e5\u4e2d\u76f4\u63a5\u5f15\u7528\u805a\u5408\u7ed3\u679c\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u89e3\u51b3\u65b9\u6cd5\uff1a<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528 <code>HAVING<\/code> \u4ee3\u66ff <code>WHERE<\/code>\uff0c\u6765\u7b5b\u9009\u805a\u5408\u540e\u7684\u6570\u636e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT department, COUNT(*) FROM employees GROUP BY department HAVING COUNT(*) &gt; 5;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u5206\u7ec4\u5b57\u6bb5\u6570\u636e\u7c7b\u578b\u95ee\u9898<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u6709\u65f6\u5019\uff0c\u5b57\u6bb5\u7684\u7c7b\u578b\u53ef\u80fd\u5bfc\u81f4\u65e0\u6cd5\u6b63\u786e\u5206\u7ec4\u3002\u4f8b\u5982\uff0c\u6587\u672c\u5b57\u6bb5\u7684\u957f\u5ea6\u53ef\u80fd\u5bfc\u81f4\u4e0d\u540c\u7684\u5206\u7ec4\uff0c\u751a\u81f3\u5728\u903b\u8f91\u4e0a\u5b83\u4eec\u5e94\u8be5\u662f\u76f8\u540c\u7684\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u89e3\u51b3\u65b9\u6cd5\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u786e\u4fdd\u5206\u7ec4\u5b57\u6bb5\u7684\u6570\u636e\u7c7b\u578b\u662f\u9002\u5f53\u7684\uff08\u5982\u6574\u6570\u3001\u65e5\u671f\u7b49\uff09\u3002\u907f\u514d\u4f7f\u7528\u975e\u5e38\u957f\u7684\u5b57\u7b26\u4e32\u8fdb\u884c\u5206\u7ec4\uff0c\u6216\u8005\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u9002\u5f53\u7684\u622a\u65ad\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong>\u68c0\u67e5\u6570\u636e\u5e93\u7684\u7248\u672c\u6216\u8bbe\u7f6e<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u6709\u4e9b MySQL \u7684\u7248\u672c\u53ef\u80fd\u5728 <code>GROUP BY<\/code> \u4e0a\u6709\u4e9b\u7279\u5b9a\u7684\u9650\u5236\u6216\u884c\u4e3a\u3002\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f\u8f83\u65e7\u7684 MySQL \u7248\u672c\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5206\u7ec4\u7684\u9650\u5236\uff08\u5982 <code>ONLY_FULL_GROUP_BY<\/code> \u6a21\u5f0f\uff09\uff0c\u5bfc\u81f4\u4e00\u4e9b\u67e5\u8be2\u65e0\u6cd5\u6b63\u5e38\u6267\u884c\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u89e3\u51b3\u65b9\u6cd5\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u53ef\u4ee5\u67e5\u770b\u5e76\u8c03\u6574 <code>ONLY_FULL_GROUP_BY<\/code> \u6a21\u5f0f\u3002\u901a\u8fc7\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\u67e5\u770b\u8be5\u6a21\u5f0f\u662f\u5426\u542f\u7528\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  SHOW VARIABLES LIKE 'sql_mode';<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u542f\u7528\u4e86 <code>ONLY_FULL_GROUP_BY<\/code>\uff0c\u4e14\u4f60\u9700\u8981\u5173\u95ed\u8be5\u6a21\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  SET sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8bf7\u6ce8\u610f\uff0c\u4fee\u6539 SQL \u6a21\u5f0f\u53ef\u80fd\u4f1a\u5f71\u54cd\u5176\u4ed6\u67e5\u8be2\u7684\u884c\u4e3a\uff0c\u56e0\u6b64\u8bf7\u8c28\u614e\u4f7f\u7528\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u5728\u4f7f\u7528 <code>GROUP BY<\/code> \u65f6\uff0c\u786e\u4fdd\uff1a<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u6240\u6709\u975e\u5206\u7ec4\u5217\u90fd\u5e94\u7528\u4e86\u805a\u5408\u51fd\u6570<\/strong>\uff0c\u6216\u8005\u88ab\u5217\u5165 <code>GROUP BY<\/code> \u5b50\u53e5\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528 <code>HAVING<\/code> \u800c\u4e0d\u662f <code>WHERE<\/code> \u8fdb\u884c\u805a\u5408\u7ed3\u679c\u7684\u7b5b\u9009\u3002<\/li>\n\n\n\n<li>\u68c0\u67e5\u5b57\u6bb5\u7684\u6570\u636e\u7c7b\u578b\uff0c\u786e\u4fdd\u9002\u5f53\u4f7f\u7528\u3002<\/li>\n\n\n\n<li>\u5728\u7279\u6b8a\u60c5\u51b5\u4e0b\uff0c\u68c0\u67e5 SQL \u6a21\u5f0f\u8bbe\u7f6e\u3002<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 MySQL \u4e2d\uff0c\u65e0\u6cd5\u8fdb\u884c\u5206\u7ec4\uff08GROUP BY\uff09\u7684\u9519\u8bef\u901a\u5e38\u4e0e\u67e5\u8be2\u4e2d\u7684\u9009\u62e9\u5b57\u6bb5\u3001\u805a\u5408\u51fd\u6570\uff08\u5982 COUNT() [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-624","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/624","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=624"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/624\/revisions"}],"predecessor-version":[{"id":625,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/624\/revisions\/625"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=624"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=624"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=624"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}