{"id":25595,"date":"2024-11-24T15:23:41","date_gmt":"2024-11-24T07:23:41","guid":{"rendered":"https:\/\/fwq.ai\/blog\/25595\/"},"modified":"2024-11-24T15:23:41","modified_gmt":"2024-11-24T07:23:41","slug":"mysql-%e4%b8%ad%e5%a6%82%e4%bd%95%e6%8c%89%e7%bb%84%e8%ae%a1%e7%ae%97%e6%8e%92%e9%99%a4%e6%9c%80%e6%96%b0%e8%ae%b0%e5%bd%95%e5%90%8e%e5%85%b6%e4%bb%96%e8%ae%b0%e5%bd%95%e7%9a%84%e6%95%b0%e5%80%bc-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/25595\/","title":{"rendered":"MySQL \u4e2d\u5982\u4f55\u6309\u7ec4\u8ba1\u7b97\u6392\u9664\u6700\u65b0\u8bb0\u5f55\u540e\u5176\u4ed6\u8bb0\u5f55\u7684\u6570\u503c\u603b\u548c\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173063719672524.jpg\" class=\"aligncenter\" title=\"MySQL \u4e2d\u5982\u4f55\u6309\u7ec4\u8ba1\u7b97\u6392\u9664\u6700\u65b0\u8bb0\u5f55\u540e\u5176\u4ed6\u8bb0\u5f55\u7684\u6570\u503c\u603b\u548c\uff1f\u63d2\u56fe\" alt=\"MySQL \u4e2d\u5982\u4f55\u6309\u7ec4\u8ba1\u7b97\u6392\u9664\u6700\u65b0\u8bb0\u5f55\u540e\u5176\u4ed6\u8bb0\u5f55\u7684\u6570\u503c\u603b\u548c\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u4e2d\u6309\u7ec4\u8ba1\u7b97\u6392\u9664\u6700\u65b0\u8bb0\u5f55\u540e\u5176\u4ed6\u8bb0\u5f55\u548c<\/strong><\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong>\u7ed9\u5b9a\u5305\u542b\u65f6\u95f4\u548c\u7c7b\u578b\u7684\u4ee5\u4e0b\u6570\u636e\u8868\uff1a<\/p>\n<pre>create table c (\n  time timestamp,\n  type varchar(255),\n  num int\n);\n\ninsert into c values\n('2023-01-01 00:00:00', 'a', 10),\n('2023-01-01 00:00:00', 'b', 12),\n('2023-01-02 00:00:00', 'a', 15),\n('2023-01-02 00:00:00', 'b', 18),\n('2023-01-03 00:00:00', 'a', 20),\n('2023-01-03 00:00:00', 'b', 22);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5982\u4f55\u5199\u4e00\u4e2amysql\u67e5\u8be2\uff0c\u6309type\u5206\u7ec4\uff0c\u8ba1\u7b97\u6392\u9664\u6700\u65b0\u4e00\u6761\u8bb0\u5f55\u540e\u5176\u4f59\u8bb0\u5f55num\u503c\u7684\u603b\u548c\uff1f<\/p>\n<p><strong>\u89e3\u7b54\uff1a<\/strong><\/p>\n<pre>SELECT t.type, SUM(t.num)\nFROM (\n    SELECT type, num, ROW_NUMBER() OVER (PARTITION BY type ORDER BY time DESC ) AS row_num\n    FROM C\n) t\nWHERE t.row_num != 1\nGROUP BY t.type<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6b64\u67e5\u8be2\u5305\u542b\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li>\u4f7f\u7528row_number()\u51fd\u6570\u4e3a\u6bcf\u4e2atype\u4e2d\u7684\u8bb0\u5f55\u5206\u914d\u4e00\u4e2a\u57fa\u4e8e\u6309\u65f6\u95f4\u964d\u5e8f\u6392\u5e8f\u7684\u5e8f\u5217\u53f7\u3002<\/li>\n<li>\u4f7f\u7528\u5b50\u67e5\u8be2\u521b\u5efa\u4e34\u65f6\u8868t\uff0c\u5176\u4e2d\u5305\u542b\u7c7b\u578b\u3001\u6570\u5b57\u548c\u5176\u4ed6\u5217\uff08\u5982\u539f\u59cb\u8868\u4e2d\u4e0d\u5b58\u5728\u7684row_num\uff09\u3002<\/li>\n<li>\u4ecet\u8868\u4e2d\u9009\u62e9\u6700\u65b0\u7684\u8bb0\u5f55\uff08row_num\u4e0d\u7b49\u4e8e1\uff09\uff0c\u5373\u6392\u9664\u6700\u65b0\u8bb0\u5f55\u3002<\/li>\n<li>\u6309type\u5206\u7ec4\u5e76\u8ba1\u7b97num\u7684\u603b\u548c\u3002<\/li>\n<\/ol>\n<p>\u4ee5\u4e0a\u5c31\u662fMySQL \u4e2d\u5982\u4f55\u6309\u7ec4\u8ba1\u7b97\u6392\u9664\u6700\u65b0\u8bb0\u5f55\u540e\u5176\u4ed6\u8bb0\u5f55\u7684\u6570\u503c\u603b\u548c\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e2d\u6309\u7ec4\u8ba1\u7b97\u6392\u9664\u6700\u65b0\u8bb0\u5f55\u540e\u5176\u4ed6\u8bb0\u5f55\u548c \u95ee\u9898\uff1a\u7ed9\u5b9a\u5305\u542b\u65f6\u95f4\u548c\u7c7b\u578b\u7684\u4ee5\u4e0b\u6570\u636e\u8868\uff1a create table c ( time timestamp, type varchar(255), num int ); insert into c values (&#8216;2023-01-01 00:00:00&#8217;, &#8216;a&#8217;, 10), (&#8216;2023-01-01 00:00:00&#8217;, &#8216;b&#8217;, 12), (&#8216;2023-01-02 00:00:00&#8217;, &#8216;a&#8217;, 15), (&#8216;2023-01-02 00:00:00&#8217;, &#8216;b&#8217;, 18), (&#8216;2023-01-03 00:00:00&#8217;, &#8216;a&#8217;, 20), (&#8216;2023-01-03 00:00:00&#8217;, &#8216;b&#8217;, 22); \u767b\u5f55\u540e\u590d\u5236 \u5982\u4f55\u5199\u4e00\u4e2amysql\u67e5\u8be2\uff0c\u6309type\u5206\u7ec4\uff0c\u8ba1\u7b97\u6392\u9664\u6700\u65b0\u4e00\u6761\u8bb0\u5f55\u540e\u5176\u4f59\u8bb0\u5f55num\u503c\u7684\u603b\u548c\uff1f \u89e3\u7b54\uff1a SELECT t.type, SUM(t.num) FROM ( SELECT type, num, ROW_NUMBER() OVER (PARTITION [&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-25595","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/25595","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=25595"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/25595\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=25595"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=25595"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=25595"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}