{"id":25422,"date":"2024-11-24T10:11:31","date_gmt":"2024-11-24T02:11:31","guid":{"rendered":"https:\/\/fwq.ai\/blog\/25422\/"},"modified":"2024-11-24T10:11:31","modified_gmt":"2024-11-24T02:11:31","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-mysql-%e6%9f%a5%e8%af%a2%e6%8e%92%e9%99%a4%e6%9c%80%e6%96%b0%e6%95%b0%e6%8d%ae%e5%90%8e%e5%90%84%e7%b1%bb%e5%9e%8b%e7%9a%84-num-%e6%80%bb%e5%92%8c%ef%bc%9f-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/25422\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 MySQL \u67e5\u8be2\u6392\u9664\u6700\u65b0\u6570\u636e\u540e\u5404\u7c7b\u578b\u7684 Num \u603b\u548c\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173060983528798.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528 MySQL \u67e5\u8be2\u6392\u9664\u6700\u65b0\u6570\u636e\u540e\u5404\u7c7b\u578b\u7684 Num \u603b\u548c\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528 MySQL \u67e5\u8be2\u6392\u9664\u6700\u65b0\u6570\u636e\u540e\u5404\u7c7b\u578b\u7684 Num \u603b\u548c\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u6309\u65f6\u95f4\u6392\u5e8f\u5e76\u6309\u7c7b\u578b\u5206\u7ec4\u540e\u8ba1\u7b97 type \u7684 num \u603b\u548c<\/strong><\/p>\n<p>\u7ed9\u5b9a\u6570\u636e\u8868\u4e2d\u5305\u542b\u6309\u65f6\u95f4\u6392\u5e8f\u548c\u6309\u7c7b\u578b\u5206\u7ec4\u7684\u6570\u636e\u3002\u8981\u8ba1\u7b97\u6392\u9664\u6700\u65b0\u4e00\u6761\u6570\u636e\u540e\uff0c\u5176\u4f59 num \u7684\u603b\u548c\uff0c\u53ef\u4ee5\u5728  \u4e2d\u4f7f\u7528\u5982\u4e0b\u67e5\u8be2\uff1a<\/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>\u8fd9\u6bb5\u67e5\u8be2\u4f7f\u7528 cte\uff08\u901a\u7528\u8868\u8868\u8fbe\u5f0f\uff09\u8fdb\u884c\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li> <strong>\u6dfb\u52a0\u884c\u53f7\uff1a<\/strong>\u4e3a\u6bcf\u4e2a\u7c7b\u578b\u6309\u65f6\u95f4\u964d\u5e8f\u8ba1\u7b97\u884c\u53f7 row_num\u3002<\/li>\n<li> <strong>\u8fc7\u6ee4\u51fa\u975e\u6700\u65b0\u6570\u636e\uff1a<\/strong>\u9009\u62e9 row_num \u4e0d\u7b49\u4e8e 1 \u7684\u884c\uff0c\u4ece\u800c\u6392\u9664\u6700\u65b0\u6570\u636e\u3002<\/li>\n<li> <strong>\u6309\u7c7b\u578b\u5206\u7ec4\u6c42\u548c\uff1a<\/strong>\u5c06\u5269\u4f59\u6570\u636e\u6309\u7c7b\u578b\u5206\u7ec4\u5e76\u8ba1\u7b97 num \u7684\u603b\u548c\u3002<\/li>\n<\/ol>\n<p>\u6700\u7ec8\uff0c\u67e5\u8be2\u8fd4\u56de\u4e86\u4e00\u4e2a\u8868\u683c\uff0c\u5176\u4e2d\u5305\u542b\u6309\u7c7b\u578b\u5206\u7ec4\u7684 num \u603b\u548c\uff0c\u6392\u9664\u4e86\u6700\u65b0\u4e00\u6761\u6570\u636e\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528 MySQL \u67e5\u8be2\u6392\u9664\u6700\u65b0\u6570\u636e\u540e\u5404\u7c7b\u578b\u7684 Num \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>\u6309\u65f6\u95f4\u6392\u5e8f\u5e76\u6309\u7c7b\u578b\u5206\u7ec4\u540e\u8ba1\u7b97 type \u7684 num \u603b\u548c \u7ed9\u5b9a\u6570\u636e\u8868\u4e2d\u5305\u542b\u6309\u65f6\u95f4\u6392\u5e8f\u548c\u6309\u7c7b\u578b\u5206\u7ec4\u7684\u6570\u636e\u3002\u8981\u8ba1\u7b97\u6392\u9664\u6700\u65b0\u4e00\u6761\u6570\u636e\u540e\uff0c\u5176\u4f59 num \u7684\u603b\u548c\uff0c\u53ef\u4ee5\u5728 \u4e2d\u4f7f\u7528\u5982\u4e0b\u67e5\u8be2\uff1a SELECT t.type, SUM(t.num) FROM ( SELECT type, num, ROW_NUMBER() OVER (PARTITION BY type ORDER BY time DESC ) AS row_num FROM C ) t WHERE t.row_num != 1 GROUP BY t.type \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u6bb5\u67e5\u8be2\u4f7f\u7528 cte\uff08\u901a\u7528\u8868\u8868\u8fbe\u5f0f\uff09\u8fdb\u884c\u4ee5\u4e0b\u6b65\u9aa4\uff1a \u6dfb\u52a0\u884c\u53f7\uff1a\u4e3a\u6bcf\u4e2a\u7c7b\u578b\u6309\u65f6\u95f4\u964d\u5e8f\u8ba1\u7b97\u884c\u53f7 row_num\u3002 \u8fc7\u6ee4\u51fa\u975e\u6700\u65b0\u6570\u636e\uff1a\u9009\u62e9 row_num \u4e0d\u7b49\u4e8e 1 \u7684\u884c\uff0c\u4ece\u800c\u6392\u9664\u6700\u65b0\u6570\u636e\u3002 \u6309\u7c7b\u578b\u5206\u7ec4\u6c42\u548c\uff1a\u5c06\u5269\u4f59\u6570\u636e\u6309\u7c7b\u578b\u5206\u7ec4\u5e76\u8ba1\u7b97 num \u7684\u603b\u548c\u3002 \u6700\u7ec8\uff0c\u67e5\u8be2\u8fd4\u56de\u4e86\u4e00\u4e2a\u8868\u683c\uff0c\u5176\u4e2d\u5305\u542b\u6309\u7c7b\u578b\u5206\u7ec4\u7684 num [&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-25422","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/25422","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=25422"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/25422\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=25422"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=25422"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=25422"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}