{"id":2340,"date":"2025-03-04T08:44:19","date_gmt":"2025-03-04T00:44:19","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2340"},"modified":"2025-03-04T08:44:19","modified_gmt":"2025-03-04T00:44:19","slug":"sqlite-group-by-%e5%ad%90%e5%8f%a5","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/04\/sqlite-group-by-%e5%ad%90%e5%8f%a5\/","title":{"rendered":"SQLite GROUP BY \u5b50\u53e5"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\"><code>GROUP BY<\/code> \u5b50\u53e5\u7528\u4e8e\u5c06\u67e5\u8be2\u7ed3\u679c\u6309\u7167\u6307\u5b9a\u5217\u8fdb\u884c\u5206\u7ec4\u3002\u5b83\u5e38\u5e38\u4e0e\u805a\u5408\u51fd\u6570\u4e00\u8d77\u4f7f\u7528\uff0c\u5982 <code>COUNT()<\/code>\u3001<code>SUM()<\/code>\u3001<code>AVG()<\/code>\u3001<code>MAX()<\/code> \u548c <code>MIN()<\/code>\uff0c\u5bf9\u6bcf\u4e2a\u5206\u7ec4\u7684\u6570\u636e\u8fdb\u884c\u8ba1\u7b97\u3002\u901a\u8fc7 <code>GROUP BY<\/code>\uff0c\u4f60\u53ef\u4ee5\u5bf9\u6bcf\u4e2a\u7ec4\u7684\u6570\u636e\u8fdb\u884c\u6c47\u603b\uff0c\u5e76\u8fd4\u56de\u6c47\u603b\u540e\u7684\u7ed3\u679c\u3002<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u57fa\u672c\u8bed\u6cd5<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT column_name(s), aggregate_function(column_name)\nFROM table_name\nWHERE condition\nGROUP BY column_name;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>column_name(s)<\/code>: \u7528\u4e8e\u5206\u7ec4\u7684\u5217\u540d\u3002<\/li>\n\n\n\n<li><code>aggregate_function<\/code>: \u805a\u5408\u51fd\u6570\uff08\u5982 <code>COUNT()<\/code>, <code>SUM()<\/code>, <code>AVG()<\/code> \u7b49\uff09\u3002<\/li>\n\n\n\n<li><code>table_name<\/code>: \u67e5\u8be2\u7684\u8868\u3002<\/li>\n\n\n\n<li><code>condition<\/code>: \u53ef\u9009\u7684\u67e5\u8be2\u6761\u4ef6\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. \u6309\u5355\u5217\u5206\u7ec4<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u6700\u5e38\u89c1\u7684\u7528\u6cd5\u662f\u901a\u8fc7\u5355\u5217\u6765\u5206\u7ec4\u6570\u636e\u3002\u805a\u5408\u51fd\u6570\u4f1a\u5bf9\u6bcf\u4e2a\u7ec4\u8fdb\u884c\u8ba1\u7b97\uff0c\u5e76\u8fd4\u56de\u6bcf\u4e2a\u5206\u7ec4\u7684\u7ed3\u679c\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6309 <code>department<\/code> \u5217\u5206\u7ec4\uff0c\u7edf\u8ba1\u6bcf\u4e2a\u90e8\u95e8\u7684\u5458\u5de5\u6570\u91cf\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  SELECT department, COUNT(*)\n  FROM employees\n  GROUP BY department;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8be5\u67e5\u8be2\u4f1a\u6839\u636e\u90e8\u95e8\u540d\u79f0 (<code>department<\/code>) \u5bf9\u5458\u5de5\u8fdb\u884c\u5206\u7ec4\uff0c\u5e76\u7edf\u8ba1\u6bcf\u4e2a\u90e8\u95e8\u7684\u5458\u5de5\u6570\u91cf\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. \u4f7f\u7528\u805a\u5408\u51fd\u6570<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>GROUP BY<\/code> \u5b50\u53e5\u901a\u5e38\u4e0e\u805a\u5408\u51fd\u6570\u7ed3\u5408\u4f7f\u7528\uff0c\u5982 <code>COUNT()<\/code>\u3001<code>SUM()<\/code>\u3001<code>AVG()<\/code>\u3001<code>MAX()<\/code> \u548c <code>MIN()<\/code> \u7b49\uff0c\u6765\u8ba1\u7b97\u6bcf\u4e2a\u5206\u7ec4\u7684\u7edf\u8ba1\u4fe1\u606f\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6309 <code>department<\/code> \u5217\u5206\u7ec4\uff0c\u7edf\u8ba1\u6bcf\u4e2a\u90e8\u95e8\u7684\u603b\u85aa\u8d44\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  SELECT department, SUM(salary)\n  FROM employees\n  GROUP BY department;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8be5\u67e5\u8be2\u8fd4\u56de\u6bcf\u4e2a\u90e8\u95e8\u7684\u603b\u85aa\u8d44\u3002<\/li>\n\n\n\n<li>\u6309 <code>department<\/code> \u5217\u5206\u7ec4\uff0c\u8ba1\u7b97\u6bcf\u4e2a\u90e8\u95e8\u7684\u5e73\u5747\u85aa\u8d44\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  SELECT department, AVG(salary)\n  FROM employees\n  GROUP BY department;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8be5\u67e5\u8be2\u8fd4\u56de\u6bcf\u4e2a\u90e8\u95e8\u7684\u5e73\u5747\u85aa\u8d44\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. \u6309\u591a\u5217\u5206\u7ec4<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>GROUP BY<\/code> \u53ef\u4ee5\u540c\u65f6\u6309\u591a\u4e2a\u5217\u5206\u7ec4\u3002\u591a\u4e2a\u5217\u6309\u987a\u5e8f\u8fdb\u884c\u5206\u7ec4\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6309 <code>department<\/code> \u548c <code>job_title<\/code> \u5217\u5206\u7ec4\uff0c\u8ba1\u7b97\u6bcf\u4e2a\u90e8\u95e8\u5185\u6bcf\u4e2a\u804c\u4f4d\u7684\u5458\u5de5\u6570\u91cf\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  SELECT department, job_title, COUNT(*)\n  FROM employees\n  GROUP BY department, job_title;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8be5\u67e5\u8be2\u8fd4\u56de\u6bcf\u4e2a\u90e8\u95e8\u548c\u804c\u4f4d\u7684\u5458\u5de5\u6570\u91cf\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. \u4f7f\u7528 <code>HAVING<\/code> \u8fc7\u6ee4\u5206\u7ec4\u7ed3\u679c<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>HAVING<\/code> \u5b50\u53e5\u7528\u4e8e\u5728\u5206\u7ec4\u540e\u5bf9\u7ed3\u679c\u8fdb\u884c\u7b5b\u9009\uff0c\u7c7b\u4f3c\u4e8e <code>WHERE<\/code> \u5b50\u53e5\uff0c\u4f46 <code>WHERE<\/code> \u4e0d\u80fd\u4e0e\u805a\u5408\u51fd\u6570\u4e00\u8d77\u4f7f\u7528\uff0c\u800c <code>HAVING<\/code> \u5b50\u53e5\u5219\u662f\u4e13\u95e8\u7528\u4e8e\u805a\u5408\u7ed3\u679c\u7684\u8fc7\u6ee4\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6309 <code>department<\/code> \u5206\u7ec4\uff0c\u8ba1\u7b97\u6bcf\u4e2a\u90e8\u95e8\u7684\u5e73\u5747\u85aa\u8d44\uff0c\u5e76\u53ea\u8fd4\u56de\u5e73\u5747\u85aa\u8d44\u5927\u4e8e 5000 \u7684\u90e8\u95e8\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  SELECT department, AVG(salary)\n  FROM employees\n  GROUP BY department\n  HAVING AVG(salary) &gt; 5000;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8be5\u67e5\u8be2\u8fd4\u56de\u85aa\u8d44\u5e73\u5747\u503c\u5927\u4e8e 5000 \u7684\u90e8\u95e8\u53ca\u5176\u5e73\u5747\u85aa\u8d44\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. \u6392\u5e8f\u5206\u7ec4\u7ed3\u679c<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>GROUP BY<\/code> \u548c <code>ORDER BY<\/code> \u53ef\u4ee5\u4e00\u8d77\u4f7f\u7528\u3002<code>GROUP BY<\/code> \u7528\u4e8e\u5206\u7ec4\uff0c<code>ORDER BY<\/code> \u7528\u4e8e\u5bf9\u5206\u7ec4\u540e\u7684\u7ed3\u679c\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6309 <code>department<\/code> \u5217\u5206\u7ec4\uff0c\u8ba1\u7b97\u6bcf\u4e2a\u90e8\u95e8\u7684\u603b\u85aa\u8d44\uff0c\u6309\u7167\u603b\u85aa\u8d44\u964d\u5e8f\u6392\u5217\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  SELECT department, SUM(salary)\n  FROM employees\n  GROUP BY department\n  ORDER BY SUM(salary) DESC;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8be5\u67e5\u8be2\u8fd4\u56de\u6309\u90e8\u95e8\u5206\u7ec4\u5e76\u6309\u603b\u85aa\u8d44\u964d\u5e8f\u6392\u5217\u7684\u7ed3\u679c\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6. \u4f7f\u7528 <code>GROUP BY<\/code> \u4e0e <code>DISTINCT<\/code><\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>GROUP BY<\/code> \u4f1a\u53bb\u9664\u91cd\u590d\u7684\u884c\uff0c\u56e0\u6b64\u53ef\u4ee5\u4e0e <code>DISTINCT<\/code> \u914d\u5408\u4f7f\u7528\u6765\u8fdb\u4e00\u6b65\u6d88\u9664\u91cd\u590d\u6570\u636e\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6309 <code>department<\/code> \u5217\u5206\u7ec4\uff0c\u627e\u51fa\u6bcf\u4e2a\u90e8\u95e8\u7684\u552f\u4e00\u804c\u4f4d\u6570\u91cf\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  SELECT department, COUNT(DISTINCT job_title)\n  FROM employees\n  GROUP BY department;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8be5\u67e5\u8be2\u8fd4\u56de\u6bcf\u4e2a\u90e8\u95e8\u5185\u4e0d\u540c\u804c\u4f4d\u7684\u6570\u91cf\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u603b\u7ed3<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>GROUP BY<\/code><\/strong> \u7528\u4e8e\u6839\u636e\u6307\u5b9a\u7684\u5217\u5c06\u7ed3\u679c\u96c6\u5206\u7ec4\u3002<\/li>\n\n\n\n<li><strong>\u805a\u5408\u51fd\u6570<\/strong> \u5982 <code>COUNT()<\/code>, <code>SUM()<\/code>, <code>AVG()<\/code>, <code>MAX()<\/code>, <code>MIN()<\/code> \u901a\u5e38\u4e0e <code>GROUP BY<\/code> \u4e00\u8d77\u4f7f\u7528\uff0c\u7528\u4e8e\u5bf9\u6bcf\u4e2a\u5206\u7ec4\u7684\u8bb0\u5f55\u8fdb\u884c\u7edf\u8ba1\u8ba1\u7b97\u3002<\/li>\n\n\n\n<li><strong><code>HAVING<\/code><\/strong> \u7528\u4e8e\u8fc7\u6ee4\u5206\u7ec4\u540e\u7684\u7ed3\u679c\uff0c\u5e38\u7528\u4e8e\u4e0e\u805a\u5408\u51fd\u6570\u4e00\u8d77\u4f7f\u7528\u3002<\/li>\n\n\n\n<li><strong><code>ORDER BY<\/code><\/strong> \u53ef\u4ee5\u4e0e <code>GROUP BY<\/code> \u4e00\u8d77\u4f7f\u7528\uff0c\u6309\u7167\u805a\u5408\u7ed3\u679c\u5bf9\u5206\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002<\/li>\n\n\n\n<li><strong>\u591a\u5217\u5206\u7ec4<\/strong> \u53ef\u4ee5\u6839\u636e\u591a\u4e2a\u5217\u8fdb\u884c\u5206\u7ec4\uff0c\u6309\u987a\u5e8f\u5904\u7406\u6bcf\u4e2a\u5217\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><code>GROUP BY<\/code> \u662f\u8fdb\u884c\u6570\u636e\u6c47\u603b\u548c\u5206\u6790\u65f6\u975e\u5e38\u91cd\u8981\u7684\u5de5\u5177\uff0c\u5c24\u5176\u5728\u9700\u8981\u751f\u6210\u6c47\u603b\u62a5\u544a\u65f6\u975e\u5e38\u6709\u7528\u3002\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>GROUP BY \u5b50\u53e5\u7528\u4e8e\u5c06\u67e5\u8be2\u7ed3\u679c\u6309\u7167\u6307\u5b9a\u5217\u8fdb\u884c\u5206\u7ec4\u3002\u5b83\u5e38\u5e38\u4e0e\u805a\u5408\u51fd\u6570\u4e00\u8d77\u4f7f\u7528\uff0c\u5982 COUNT()\u3001SUM( [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[76],"tags":[],"class_list":["post-2340","post","type-post","status-publish","format-standard","hentry","category-sqlite"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2340","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=2340"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2340\/revisions"}],"predecessor-version":[{"id":2341,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2340\/revisions\/2341"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2340"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2340"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2340"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}