{"id":6150,"date":"2024-11-14T08:22:00","date_gmt":"2024-11-14T00:22:00","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6150\/"},"modified":"2024-11-14T08:22:00","modified_gmt":"2024-11-14T00:22:00","slug":"mysql%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e7%aa%97%e5%8f%a3%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6150\/","title":{"rendered":"mysql\u5982\u4f55\u4f7f\u7528\u7a97\u53e3\u51fd\u6570"},"content":{"rendered":"<blockquote><p>\n  \u4f7f\u7528 mysql \u7a97\u53e3\u51fd\u6570\u7684\u6b65\u9aa4\uff1a1. \u5b9a\u4e49\u7a97\u53e3\uff08over \u5173\u952e\u5b57\uff09\u30012. \u9009\u62e9\u7a97\u53e3\u51fd\u6570\uff08sum()\u3001avg()\u3001rank()\uff09\u30013. \u5e94\u7528\u7a97\u53e3\u51fd\u6570\u3002\u7a97\u53e3\u51fd\u6570\u7c7b\u578b\u6709\u5206\u7ec4\u3001\u6392\u5e8f\u3001\u884c\u3001\u7d2f\u8ba1\u3002\u7a97\u53e3\u8303\u56f4\u6307\u5b9a\u7a97\u53e3\u5185\u884c\u6570\uff08rows between\uff09\u6216\u884c\u503c\u8303\u56f4\uff08range between\uff09\uff0c\u6216\u4ece\u8d77\u59cb\u884c\u5230\u5f53\u524d\u884c\uff08unbounded preceding\uff09\u6216\u4ece\u5f53\u524d\u884c\u5230\u6700\u540e\uff08unbounded following\uff09\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/14\/2024061402423548947.jpg\" class=\"aligncenter\" title=\"mysql\u5982\u4f55\u4f7f\u7528\u7a97\u53e3\u51fd\u6570\u63d2\u56fe\" alt=\"mysql\u5982\u4f55\u4f7f\u7528\u7a97\u53e3\u51fd\u6570\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 MySQL \u4e2d\u7684\u7a97\u53e3\u51fd\u6570<\/strong><\/p>\n<p>\u7a97\u53e3\u51fd\u6570\u662f SQL \u4e2d\u7528\u4e8e\u5bf9\u6570\u636e\u96c6\u5185\u884c\u6570\u636e\u8fdb\u884c\u805a\u5408\u8ba1\u7b97\u7684\u51fd\u6570\u3002\u5b83\u4eec\u5728\u7a97\u53e3\u5185\u8fdb\u884c\u64cd\u4f5c\uff0c\u7a97\u53e3\u7531\u884c\u503c\u4e4b\u95f4\u7684\u7279\u5b9a\u987a\u5e8f\u6216\u5206\u7ec4\u5b9a\u4e49\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528\u7a97\u53e3\u51fd\u6570<\/strong><\/p>\n<p>\u4f7f\u7528\u7a97\u53e3\u51fd\u6570\u9700\u8981\u4e09\u4e2a\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li> <strong>\u5b9a\u4e49\u7a97\u53e3\uff1a<\/strong>\u4f7f\u7528 OVER \u5173\u952e\u5b57\u5b9a\u4e49\u7a97\u53e3\uff0c\u5b83\u6307\u5b9a\u7a97\u53e3\u7684\u8303\u56f4\u548c\u6392\u5e8f\u987a\u5e8f\u3002<\/li>\n<li> <strong>\u9009\u62e9\u7a97\u53e3\u51fd\u6570\uff1a<\/strong>\u9009\u62e9\u9002\u5408\u8ba1\u7b97\u7684\u7a97\u53e3\u51fd\u6570\uff0c\u4f8b\u5982 SUM()\u3001AVG() \u6216 `RANK()\u201d\u3002<\/li>\n<li> <strong>\u5e94\u7528\u7a97\u53e3\u51fd\u6570\uff1a<\/strong>\u5c06\u7a97\u53e3\u51fd\u6570\u5e94\u7528\u4e8e\u6570\u636e\uff0c\u4f5c\u4e3a\u67e5\u8be2\u7684\u4e00\u90e8\u5206\u3002<\/li>\n<\/ol>\n<p><strong>\u7a97\u53e3\u51fd\u6570\u793a\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u793a\u4f8b\u6f14\u793a\u5982\u4f55\u4f7f\u7528 SUM() \u7a97\u53e3\u51fd\u6570\u8ba1\u7b97\u6bcf\u4e2a\u7ec4\u5185\u7684\u884c\u603b\u548c\uff1a<\/p>\n<pre>SELECT department_id,\n       SUM(salary) OVER (PARTITION BY department_id) AS total_salary\nFROM employee;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u7a97\u53e3\u51fd\u6570\u7c7b\u578b<\/strong><\/p>\n<p>MySQL \u4e2d\u6709\u56db\u79cd\u4e3b\u8981\u7684\u7a97\u53e3\u51fd\u6570\u7c7b\u578b\uff1a<\/p>\n<ul>\n<li> <strong>\u5206\u7ec4\u7a97\u53e3\u51fd\u6570\uff1a<\/strong>\u6839\u636e\u7279\u5b9a\u5217\u5206\u7ec4\u5bf9\u6570\u636e\u8fdb\u884c\u805a\u5408\uff0c\u4f8b\u5982 SUM() \u548c `AVG()\u201d\u3002<\/li>\n<li> <strong>\u6392\u5e8f\u7a97\u53e3\u51fd\u6570\uff1a<\/strong>\u6839\u636e\u7279\u5b9a\u5217\u6392\u5e8f\u6570\u636e\uff0c\u5e76\u6839\u636e\u8be5\u987a\u5e8f\u8fdb\u884c\u805a\u5408\uff0c\u4f8b\u5982 RANK() \u548c `DENSE_RANK()\u201d\u3002<\/li>\n<li> <strong>\u884c\u7a97\u53e3\u51fd\u6570\uff1a<\/strong>\u5bf9\u5f53\u524d\u884c\u4e0e\u5176\u76f8\u90bb\u884c\u6267\u884c\u805a\u5408\uff0c\u4f8b\u5982 LAG() \u548c `LEAD()\u201d\u3002<\/li>\n<li> <strong>\u7d2f\u8ba1\u7a97\u53e3\u51fd\u6570\uff1a<\/strong>\u8ba1\u7b97\u8de8\u884c\u7d2f\u79ef\u503c\uff0c\u4f8b\u5982 SUM() \u548c AVG() \u7684 OVER (ORDER BY .. ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)\u3002<\/li>\n<\/ul>\n<p><strong>\u7a97\u53e3\u8303\u56f4<\/strong><\/p>\n<p>\u7a97\u53e3\u8303\u56f4\u6307\u5b9a\u7a97\u53e3\u4e2d\u8981\u5305\u62ec\u7684\u884c\uff0c\u53ef\u4ee5\u662f\uff1a<\/p>\n<ul>\n<li> <strong>\u8303\u56f4\uff1a<\/strong>\u6307\u5b9a\u884c\u6570\u8303\u56f4\uff0c\u4f8b\u5982 ROWS BETWEEN 3 PRECEDING AND 3 FOLLOWING\u3002<\/li>\n<li> <strong>\u533a\u95f4\uff1a<\/strong>\u6307\u5b9a\u884c\u503c\u8303\u56f4\uff0c\u4f8b\u5982 RANGE BETWEEN INTERVAL 1 DAY PRECEDING AND INTERVAL 1 DAY FOLLOWING\u3002<\/li>\n<li> <strong>\u65e0\u754c\uff1a<\/strong>\u4ece\u5f00\u59cb\u5230\u5f53\u524d\u884c\uff08UNBOUNDED PRECEDING\uff09\u6216\u4ece\u5f53\u524d\u884c\u5230\u6700\u540e\uff08UNBOUNDED FOLLOWING\uff09\u3002<\/li>\n<\/ul>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<ul>\n<li>\u7a97\u53e3\u51fd\u6570\u4e0d\u80fd\u4e0e GROUP BY\u3001HAVING \u6216 DISTINCT \u8bed\u53e5\u4e00\u8d77\u4f7f\u7528\u3002<\/li>\n<li>\u7a97\u53e3\u51fd\u6570\u53ef\u80fd\u4f1a\u5f71\u54cd\u67e5\u8be2\u6027\u80fd\uff0c\u5e94\u8c28\u614e\u4f7f\u7528\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528\u7a97\u53e3\u51fd\u6570\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528 mysql \u7a97\u53e3\u51fd\u6570\u7684\u6b65\u9aa4\uff1a1. \u5b9a\u4e49\u7a97\u53e3\uff08over \u5173\u952e\u5b57\uff09\u30012. \u9009\u62e9\u7a97\u53e3\u51fd\u6570\uff08sum()\u3001avg()\u3001rank()\uff09\u30013. \u5e94\u7528\u7a97\u53e3\u51fd\u6570\u3002\u7a97\u53e3\u51fd\u6570\u7c7b\u578b\u6709\u5206\u7ec4\u3001\u6392\u5e8f\u3001\u884c\u3001\u7d2f\u8ba1\u3002\u7a97\u53e3\u8303\u56f4\u6307\u5b9a\u7a97\u53e3\u5185\u884c\u6570\uff08rows between\uff09\u6216\u884c\u503c\u8303\u56f4\uff08range between\uff09\uff0c\u6216\u4ece\u8d77\u59cb\u884c\u5230\u5f53\u524d\u884c\uff08unbounded preceding\uff09\u6216\u4ece\u5f53\u524d\u884c\u5230\u6700\u540e\uff08unbounded following\uff09\u3002 \u5982\u4f55\u4f7f\u7528 MySQL \u4e2d\u7684\u7a97\u53e3\u51fd\u6570 \u7a97\u53e3\u51fd\u6570\u662f SQL \u4e2d\u7528\u4e8e\u5bf9\u6570\u636e\u96c6\u5185\u884c\u6570\u636e\u8fdb\u884c\u805a\u5408\u8ba1\u7b97\u7684\u51fd\u6570\u3002\u5b83\u4eec\u5728\u7a97\u53e3\u5185\u8fdb\u884c\u64cd\u4f5c\uff0c\u7a97\u53e3\u7531\u884c\u503c\u4e4b\u95f4\u7684\u7279\u5b9a\u987a\u5e8f\u6216\u5206\u7ec4\u5b9a\u4e49\u3002 \u5982\u4f55\u4f7f\u7528\u7a97\u53e3\u51fd\u6570 \u4f7f\u7528\u7a97\u53e3\u51fd\u6570\u9700\u8981\u4e09\u4e2a\u6b65\u9aa4\uff1a \u5b9a\u4e49\u7a97\u53e3\uff1a\u4f7f\u7528 OVER \u5173\u952e\u5b57\u5b9a\u4e49\u7a97\u53e3\uff0c\u5b83\u6307\u5b9a\u7a97\u53e3\u7684\u8303\u56f4\u548c\u6392\u5e8f\u987a\u5e8f\u3002 \u9009\u62e9\u7a97\u53e3\u51fd\u6570\uff1a\u9009\u62e9\u9002\u5408\u8ba1\u7b97\u7684\u7a97\u53e3\u51fd\u6570\uff0c\u4f8b\u5982 SUM()\u3001AVG() \u6216 `RANK()\u201d\u3002 \u5e94\u7528\u7a97\u53e3\u51fd\u6570\uff1a\u5c06\u7a97\u53e3\u51fd\u6570\u5e94\u7528\u4e8e\u6570\u636e\uff0c\u4f5c\u4e3a\u67e5\u8be2\u7684\u4e00\u90e8\u5206\u3002 \u7a97\u53e3\u51fd\u6570\u793a\u4f8b \u4ee5\u4e0b\u793a\u4f8b\u6f14\u793a\u5982\u4f55\u4f7f\u7528 SUM() \u7a97\u53e3\u51fd\u6570\u8ba1\u7b97\u6bcf\u4e2a\u7ec4\u5185\u7684\u884c\u603b\u548c\uff1a SELECT department_id, SUM(salary) OVER (PARTITION BY department_id) AS total_salary FROM employee; \u767b\u5f55\u540e\u590d\u5236 \u7a97\u53e3\u51fd\u6570\u7c7b\u578b MySQL \u4e2d\u6709\u56db\u79cd\u4e3b\u8981\u7684\u7a97\u53e3\u51fd\u6570\u7c7b\u578b\uff1a \u5206\u7ec4\u7a97\u53e3\u51fd\u6570\uff1a\u6839\u636e\u7279\u5b9a\u5217\u5206\u7ec4\u5bf9\u6570\u636e\u8fdb\u884c\u805a\u5408\uff0c\u4f8b\u5982 SUM() \u548c `AVG()\u201d\u3002 \u6392\u5e8f\u7a97\u53e3\u51fd\u6570\uff1a\u6839\u636e\u7279\u5b9a\u5217\u6392\u5e8f\u6570\u636e\uff0c\u5e76\u6839\u636e\u8be5\u987a\u5e8f\u8fdb\u884c\u805a\u5408\uff0c\u4f8b\u5982 RANK() \u548c `DENSE_RANK()\u201d\u3002 \u884c\u7a97\u53e3\u51fd\u6570\uff1a\u5bf9\u5f53\u524d\u884c\u4e0e\u5176\u76f8\u90bb\u884c\u6267\u884c\u805a\u5408\uff0c\u4f8b\u5982 [&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-6150","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6150","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=6150"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6150\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6150"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6150"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6150"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}