{"id":76196,"date":"2024-10-16T04:39:16","date_gmt":"2024-10-15T20:39:16","guid":{"rendered":"https:\/\/server.hk\/cnblog\/76196\/"},"modified":"2024-10-16T04:39:16","modified_gmt":"2024-10-15T20:39:16","slug":"mysql%e5%85%a5%e9%96%80%ef%bc%9a%e4%bd%bf%e7%94%a8avg%e5%87%bd%e6%95%b8%e8%bf%94%e5%9b%9e%e6%95%b8%e5%80%bc%e5%88%97%e7%9a%84%e5%b9%b3%e5%9d%87%e5%80%bc%e3%80%82","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/76196\/","title":{"rendered":"MySQL\u5165\u9580\uff1a\u4f7f\u7528AVG()\u51fd\u6578\u8fd4\u56de\u6578\u503c\u5217\u7684\u5e73\u5747\u503c\u3002"},"content":{"rendered":"<h1 id=\"mysql%e5%85%a5%e9%96%80%ef%bc%9a%e4%bd%bf%e7%94%a8avg%e5%87%bd%e6%95%b8%e8%bf%94%e5%9b%9e%e6%95%b8%e5%80%bc%e5%88%97%e7%9a%84%e5%b9%b3%e5%9d%87%e5%80%bc-SZeEYXfCuI\">MySQL\u5165\u9580\uff1a\u4f7f\u7528AVG()\u51fd\u6578\u8fd4\u56de\u6578\u503c\u5217\u7684\u5e73\u5747\u503c<\/h1>\n<p>\u5728\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\u4e2d\uff0cMySQL\u662f\u4e00\u500b\u975e\u5e38\u6d41\u884c\u7684\u9078\u64c7\uff0c\u56e0\u70ba\u5b83\u7684\u9ad8\u6548\u80fd\u548c\u9748\u6d3b\u6027\u3002\u5c0d\u65bc\u8a31\u591a\u958b\u767c\u8005\u548c\u6578\u64da\u5206\u6790\u5e2b\u4f86\u8aaa\uff0c\u80fd\u5920\u6709\u6548\u5730\u5f9e\u6578\u64da\u4e2d\u63d0\u53d6\u6709\u7528\u7684\u4fe1\u606f\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u5c07\u6df1\u5165\u63a2\u8a0eMySQL\u4e2d\u7684AVG()\u51fd\u6578\uff0c\u9019\u662f\u4e00\u500b\u7528\u65bc\u8a08\u7b97\u6578\u503c\u5217\u5e73\u5747\u503c\u7684\u5f37\u5927\u5de5\u5177\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afavg%e5%87%bd%e6%95%b8%ef%bc%9f-SZeEYXfCuI\">\u4ec0\u9ebc\u662fAVG()\u51fd\u6578\uff1f<\/h2>\n<p>AVG()\u51fd\u6578\u662fMySQL\u4e2d\u7684\u4e00\u500b\u805a\u5408\u51fd\u6578\uff0c\u7528\u65bc\u8a08\u7b97\u6307\u5b9a\u5217\u7684\u5e73\u5747\u503c\u3002\u9019\u500b\u51fd\u6578\u53ef\u4ee5\u5728SELECT\u67e5\u8a62\u4e2d\u4f7f\u7528\uff0c\u4e26\u4e14\u901a\u5e38\u8207GROUP BY\u5b50\u53e5\u4e00\u8d77\u4f7f\u7528\uff0c\u4ee5\u4fbf\u5c0d\u5206\u7d44\u6578\u64da\u9032\u884c\u8a08\u7b97\u3002<\/p>\n<h2 id=\"avg%e5%87%bd%e6%95%b8%e7%9a%84%e8%aa%9e%e6%b3%95-SZeEYXfCuI\">AVG()\u51fd\u6578\u7684\u8a9e\u6cd5<\/h2>\n<pre><code>AVG(expression)<\/code><\/pre>\n<p>\u5728\u9019\u88e1\uff0c<code>expression<\/code>\u662f\u8981\u8a08\u7b97\u5e73\u5747\u503c\u7684\u6578\u503c\u5217\u3002\u9019\u500b\u5217\u53ef\u4ee5\u662f\u8868\u4e2d\u7684\u4e00\u500b\u5b57\u6bb5\uff0c\u4e5f\u53ef\u4ee5\u662f\u8a08\u7b97\u51fa\u7684\u503c\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8avg%e5%87%bd%e6%95%b8%e7%9a%84%e7%af%84%e4%be%8b-SZeEYXfCuI\">\u4f7f\u7528AVG()\u51fd\u6578\u7684\u7bc4\u4f8b<\/h2>\n<p>\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b\u540d\u70ba<code>students<\/code>\u7684\u8868\uff0c\u8a72\u8868\u5305\u542b\u5b78\u751f\u7684\u6210\u7e3e\u4fe1\u606f\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre><code>CREATE TABLE students (\n    id INT AUTO_INCREMENT PRIMARY KEY,\n    name VARCHAR(100),\n    score DECIMAL(5,2)\n);\n<\/code><\/pre>\n<p>\u6211\u5011\u53ef\u4ee5\u63d2\u5165\u4e00\u4e9b\u6578\u64da\uff1a<\/p>\n<pre><code>INSERT INTO students (name, score) VALUES\n('Alice', 85.50),\n('Bob', 90.00),\n('Charlie', 78.00),\n('David', 88.00);\n<\/code><\/pre>\n<p>\u73fe\u5728\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528AVG()\u51fd\u6578\u4f86\u8a08\u7b97\u6240\u6709\u5b78\u751f\u7684\u5e73\u5747\u6210\u7e3e\uff1a<\/p>\n<pre><code>SELECT AVG(score) AS average_score FROM students;\n<\/code><\/pre>\n<p>\u57f7\u884c\u9019\u500b\u67e5\u8a62\u5f8c\uff0c\u5c07\u8fd4\u56de\u6240\u6709\u5b78\u751f\u6210\u7e3e\u7684\u5e73\u5747\u503c\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8avg%e5%87%bd%e6%95%b8%e8%88%87group-by-SZeEYXfCuI\">\u4f7f\u7528AVG()\u51fd\u6578\u8207GROUP BY<\/h2>\n<p>AVG()\u51fd\u6578\u4e5f\u53ef\u4ee5\u8207GROUP BY\u5b50\u53e5\u4e00\u8d77\u4f7f\u7528\uff0c\u4ee5\u4fbf\u5c0d\u4e0d\u540c\u7d44\u7684\u6578\u64da\u9032\u884c\u8a08\u7b97\u3002\u4f8b\u5982\uff0c\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b\u540d\u70ba<code>courses<\/code>\u7684\u8868\uff0c\u8a18\u9304\u4e86\u5b78\u751f\u7684\u8ab2\u7a0b\u548c\u6210\u7e3e\uff1a<\/p>\n<pre><code>CREATE TABLE courses (\n    id INT AUTO_INCREMENT PRIMARY KEY,\n    student_id INT,\n    course_name VARCHAR(100),\n    score DECIMAL(5,2),\n    FOREIGN KEY (student_id) REFERENCES students(id)\n);\n<\/code><\/pre>\n<p>\u6211\u5011\u53ef\u4ee5\u63d2\u5165\u4e00\u4e9b\u6578\u64da\uff1a<\/p>\n<pre><code>INSERT INTO courses (student_id, course_name, score) VALUES\n(1, 'Math', 85.00),\n(1, 'English', 90.00),\n(2, 'Math', 78.00),\n(2, 'English', 88.00);\n<\/code><\/pre>\n<p>\u73fe\u5728\uff0c\u6211\u5011\u53ef\u4ee5\u8a08\u7b97\u6bcf\u500b\u5b78\u751f\u5728\u4e0d\u540c\u8ab2\u7a0b\u4e2d\u7684\u5e73\u5747\u6210\u7e3e\uff1a<\/p>\n<pre><code>SELECT student_id, AVG(score) AS average_score\nFROM courses\nGROUP BY student_id;\n<\/code><\/pre>\n<p>\u9019\u6a23\uff0c\u6211\u5011\u5c31\u80fd\u5920\u770b\u5230\u6bcf\u4f4d\u5b78\u751f\u7684\u5e73\u5747\u6210\u7e3e\uff0c\u9019\u5c0d\u65bc\u5206\u6790\u5b78\u751f\u7684\u5b78\u7fd2\u60c5\u6cc1\u975e\u5e38\u6709\u5e6b\u52a9\u3002<\/p>\n<h2 id=\"%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85-SZeEYXfCuI\">\u6ce8\u610f\u4e8b\u9805<\/h2>\n<ul>\n<li>AVG()\u51fd\u6578\u53ea\u8a08\u7b97\u975eNULL\u503c\u3002\u5982\u679c\u5217\u4e2d\u6709NULL\u503c\uff0c\u9019\u4e9b\u503c\u5c07\u88ab\u5ffd\u7565\u3002<\/li>\n<li>\u5728\u4f7f\u7528AVG()\u51fd\u6578\u6642\uff0c\u78ba\u4fdd\u6578\u64da\u985e\u578b\u70ba\u6578\u503c\u578b\uff0c\u5426\u5247\u5c07\u6703\u51fa\u73fe\u932f\u8aa4\u3002<\/li>\n<li>\u5728\u9032\u884c\u5927\u6578\u64da\u96c6\u7684\u8a08\u7b97\u6642\uff0c\u53ef\u80fd\u6703\u5f71\u97ff\u67e5\u8a62\u6027\u80fd\uff0c\u56e0\u6b64\u5efa\u8b70\u5c0d\u6578\u64da\u9032\u884c\u9069\u7576\u7684\u7d22\u5f15\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-SZeEYXfCuI\">\u7e3d\u7d50<\/h2>\n<p>AVG()\u51fd\u6578\u662fMySQL\u4e2d\u4e00\u500b\u975e\u5e38\u5be6\u7528\u7684\u5de5\u5177\uff0c\u80fd\u5920\u5e6b\u52a9\u7528\u6236\u5feb\u901f\u8a08\u7b97\u6578\u503c\u5217\u7684\u5e73\u5747\u503c\u3002\u7121\u8ad6\u662f\u5728\u5b78\u8853\u7814\u7a76\u9084\u662f\u5546\u696d\u5206\u6790\u4e2d\uff0c\u9019\u500b\u51fd\u6578\u90fd\u80fd\u63d0\u4f9b\u6709\u50f9\u503c\u7684\u898b\u89e3\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6548\u6578\u64da\u8655\u7406\u7684\u7528\u6236\uff0c\u9078\u64c7\u5408\u9069\u7684<code><a href=\"https:\/\/server.hk\">VPS<\/a><\/code>\u89e3\u6c7a\u65b9\u6848\u5c07\u6709\u52a9\u65bc\u63d0\u5347\u6578\u64da\u5eab\u7684\u6027\u80fd\u548c\u7a69\u5b9a\u6027\u3002\u4e86\u89e3\u5982\u4f55\u4f7f\u7528MySQL\u7684\u5404\u7a2e\u529f\u80fd\uff0c\u5c07\u4f7f\u60a8\u5728\u6578\u64da\u7ba1\u7406\u548c\u5206\u6790\u65b9\u9762\u66f4\u52a0\u5f97\u5fc3\u61c9\u624b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u5728MySQL\u4e2d\u4f7f\u7528AVG()\u51fd\u6578\u8a08\u7b97\u6578\u503c\u5217\u7684\u5e73\u5747\u503c\uff0c\u638c\u63e1\u57fa\u672c\u6578\u64da\u5206\u6790\u6280\u5de7\uff0c\u63d0\u5347\u6578\u64da\u8655\u7406\u80fd\u529b\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-76196","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/76196","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=76196"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/76196\/revisions"}],"predecessor-version":[{"id":76197,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/76196\/revisions\/76197"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=76196"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=76196"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=76196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}