{"id":236,"date":"2023-02-24T05:57:06","date_gmt":"2023-02-23T21:57:06","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=236"},"modified":"2023-04-30T15:21:23","modified_gmt":"2023-04-30T07:21:23","slug":"mysql-grouping","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/24\/mysql-grouping\/","title":{"rendered":"MySQL \u5206\u7ec4"},"content":{"rendered":"<p><code>GROUP BY<\/code>\u8bed\u53e5\u6839\u636e\u4e00\u4e2a\u6216\u591a\u4e2a\u5217\u5bf9\u7ed3\u679c\u96c6\u8fdb\u884c\u5206\u7ec4\u3002<\/p>\n<p>\u5728\u5206\u7ec4\u7684\u5217\u4e0a\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>COUNT<\/code>, <code>SUM<\/code>, <code>AVG<\/code>\u7b49\u51fd\u6570\u3002<\/p>\n<h2>GROUP BY \u8bed\u6cd5<\/h2>\n<p><!-- more --><\/p>\n<pre><code class=\"language-sql\">SELECT column_name, function(column_name)\nFROM table_name\nWHERE column_name operator value\nGROUP BY column_name;<\/code><\/pre>\n<h2>\u5b9e\u4f8b\u6f14\u793a<\/h2>\n<p>\u672c\u5b9e\u4f8b\u4f7f\u7528\u5230\u4ee5\u4e0b\u8868\u7ed3\u6784\u53ca\u6570\u636e\uff0c\u4f7f\u7528\u524d\u6211\u4eec\u53ef\u4ee5\u5148\u5c06\u4ee5\u4e0b\u6570\u636e\u5bfc\u5165\u6570\u636e\u5e93\u4e2d\u3002<\/p>\n<pre><code class=\"language-sql\">SET NAMES utf8;\nSET FOREIGN_KEY_CHECKS = 0;\n\n-- ----------------------------\n--  Table structure for `employee_tbl`\n-- ----------------------------\nDROP TABLE IF EXISTS `employee_tbl`;\nCREATE TABLE `employee_tbl` (\n  `id` int(11) NOT NULL,\n  `name` char(10) NOT NULL DEFAULT &#039;&#039;,\n  `date` datetime NOT NULL,\n  `singin` tinyint(4) NOT NULL DEFAULT &#039;0&#039; COMMENT &#039;\u767b\u5f55\u6b21\u6570&#039;,\n  PRIMARY KEY (`id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8;\n\n-- ----------------------------\n--  Records of `employee_tbl`\n-- ----------------------------\nBEGIN;\nINSERT INTO `employee_tbl` VALUES (&#039;1&#039;, &#039;\u5c0f\u660e&#039;, &#039;2016-04-22 15:25:33&#039;, &#039;1&#039;), (&#039;2&#039;, &#039;\u5c0f\u53f6&#039;, &#039;2016-04-20 15:25:47&#039;, &#039;3&#039;), (&#039;3&#039;, &#039;\u5c0f\u82b3&#039;, &#039;2016-04-19 15:26:02&#039;, &#039;2&#039;), (&#039;4&#039;, &#039;\u5c0f\u53f6&#039;, &#039;2016-04-07 15:26:14&#039;, &#039;4&#039;), (&#039;5&#039;, &#039;\u5c0f\u660e&#039;, &#039;2016-04-11 15:26:40&#039;, &#039;4&#039;), (&#039;6&#039;, &#039;\u5c0f\u660e&#039;, &#039;2016-04-04 15:26:54&#039;, &#039;2&#039;);\nCOMMIT;\n\nSET FOREIGN_KEY_CHECKS = 1;<\/code><\/pre>\n<p>\u5bfc\u5165\u6210\u529f\u540e\uff0c\u6267\u884c\u4ee5\u4e0b SQL \u8bed\u53e5\uff1a<\/p>\n<pre><code class=\"language-sql\">mysql&gt; set names utf8;\nmysql&gt; SELECT * FROM employee_tbl;\n+----+--------+---------------------+--------+\n| id | name   | date                | singin |\n+----+--------+---------------------+--------+\n|  1 | \u5c0f\u660e   | 2016-04-22 15:25:33 |      1 |\n|  2 | \u5c0f\u738b   | 2016-04-20 15:25:47 |      3 |\n|  3 | \u5c0f\u4e3d   | 2016-04-19 15:26:02 |      2 |\n|  4 | \u5c0f\u738b   | 2016-04-07 15:26:14 |      4 |\n|  5 | \u5c0f\u660e   | 2016-04-11 15:26:40 |      4 |\n|  6 | \u5c0f\u660e   | 2016-04-04 15:26:54 |      2 |\n+----+--------+---------------------+--------+\n6 rows in set (0.00 sec)<\/code><\/pre>\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u4f7f\u7528<code>GROUP BY<\/code>\u8bed\u53e5\u5c06\u6570\u636e\u8868\u6309\u540d\u5b57\u8fdb\u884c\u5206\u7ec4\uff0c\u5e76\u7edf\u8ba1\u6bcf\u4e2a\u4eba\u6709\u591a\u5c11\u6761\u8bb0\u5f55\uff1a<\/p>\n<pre><code class=\"language-sql\">mysql&gt; SELECT name, COUNT(*) FROM employee_tbl GROUP BY name;\n+--------+----------+\n| name   | COUNT(*) |\n+--------+----------+\n| \u5c0f\u82b3   |        1 |\n| \u5c0f\u660e   |        3 |\n| \u5c0f\u53f6   |        2 |\n+--------+----------+\n3 rows in set (0.01 sec)<\/code><\/pre>\n<h2>\u4f7f\u7528 WITH ROLLUP<\/h2>\n<p><code>WITH ROLLUP<\/code>\u53ef\u4ee5\u5b9e\u73b0\u5728\u5206\u7ec4\u7edf\u8ba1\u6570\u636e\u57fa\u7840\u4e0a\u518d\u8fdb\u884c\u76f8\u540c\u7684\u7edf\u8ba1\uff08SUM,AVG,COUNT\u2026\uff09\u3002<\/p>\n<p>\u4f8b\u5982\u6211\u4eec\u5c06\u4ee5\u4e0a\u7684\u6570\u636e\u8868\u6309\u540d\u5b57\u8fdb\u884c\u5206\u7ec4\uff0c\u518d\u7edf\u8ba1\u6bcf\u4e2a\u4eba\u767b\u5f55\u7684\u6b21\u6570\uff1a<\/p>\n<pre><code class=\"language-sql\">mysql&gt; SELECT name, SUM(singin) as singin_count FROM employee_tbl GROUP BY name WITH ROLLUP;\n+--------+--------------+\n| name   | singin_count |\n+--------+--------------+\n| \u5c0f\u4e3d   |            2 |\n| \u5c0f\u660e   |            7 |\n| \u5c0f\u738b   |            7 |\n| NULL   |           16 |\n+--------+--------------+\n4 rows in set (0.00 sec)<\/code><\/pre>\n<p>\u5176\u4e2d\u8bb0\u5f55<code>NULL<\/code>\u8868\u793a\u6240\u6709\u4eba\u7684\u767b\u5f55\u6b21\u6570\u3002<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>coalesce<\/code>\u6765\u8bbe\u7f6e\u4e00\u4e2a\u53ef\u4ee5\u53d6\u4ee3<code>NUll<\/code>\u7684\u540d\u79f0\uff0c<code>coalesce<\/code>\u8bed\u6cd5\uff1a<\/p>\n<pre><code class=\"language-sql\">select coalesce(a,b,c);<\/code><\/pre>\n<p>\u53c2\u6570\u8bf4\u660e\uff1a\u5982\u679c<code>a==null<\/code>\uff0c\u5219\u9009\u62e9<code>b<\/code>\uff1b\u5982\u679c<code>b==null<\/code>\uff0c\u5219\u9009\u62e9<code>c<\/code>\uff1b\u5982\u679c<code>a!=null<\/code>\uff0c\u5219\u9009\u62e9<code>a<\/code>\uff1b\u5982\u679c<code>a b c<\/code>\u90fd\u4e3a<code>null<\/code>\uff0c\u5219\u8fd4\u56de\u4e3a<code>null<\/code>\uff08\u6ca1\u610f\u4e49\uff09<\/p>\n<p>\u4ee5\u4e0b\u5b9e\u4f8b\u4e2d\u5982\u679c\u540d\u5b57\u4e3a\u7a7a\u6211\u4eec\u4f7f\u7528\u603b\u6570\u4ee3\u66ff\uff1a<\/p>\n<pre><code class=\"language-sql\">mysql&gt; SELECT coalesce(name, &#039;\u603b\u6570&#039;), SUM(singin) as singin_count FROM  employee_tbl GROUP BY name WITH ROLLUP;\n+--------------------------+--------------+\n| coalesce(name, &#039;\u603b\u6570&#039;)   | singin_count |\n+--------------------------+--------------+\n| \u5c0f\u4e3d                     |            2 |\n| \u5c0f\u660e                     |            7 |\n| \u5c0f\u738b                     |            7 |\n| \u603b\u6570                     |           16 |\n+--------------------------+--------------+\n4 rows in set (0.01 sec)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>GROUP BY\u8bed\u53e5\u6839\u636e\u4e00\u4e2a\u6216\u591a\u4e2a\u5217\u5bf9\u7ed3\u679c\u96c6\u8fdb\u884c\u5206\u7ec4\u3002 \u5728\u5206\u7ec4\u7684\u5217\u4e0a\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528COUNT, SUM, AVG [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-236","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/236","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=236"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/236\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=236"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=236"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=236"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}