{"id":7205,"date":"2024-11-14T13:56:49","date_gmt":"2024-11-14T05:56:49","guid":{"rendered":"https:\/\/fwq.ai\/blog\/7205\/"},"modified":"2024-11-14T13:56:49","modified_gmt":"2024-11-14T05:56:49","slug":"%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8-mysql-%e8%ae%a1%e7%ae%97%e6%95%b0%e6%8d%ae%e8%a1%a8%e4%b8%ad%e6%8e%92%e9%99%a4%e6%9c%80%e6%96%b0%e4%b8%80%e6%9d%a1%e8%ae%b0%e5%bd%95%e5%90%8e%e5%85%b6%e4%bd%99","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/7205\/","title":{"rendered":"\u5982\u4f55\u5229\u7528 MySQL \u8ba1\u7b97\u6570\u636e\u8868\u4e2d\u6392\u9664\u6700\u65b0\u4e00\u6761\u8bb0\u5f55\u540e\u5176\u4f59\u8bb0\u5f55\u7684\u603b\u548c\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173060838498835.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5229\u7528 MySQL \u8ba1\u7b97\u6570\u636e\u8868\u4e2d\u6392\u9664\u6700\u65b0\u4e00\u6761\u8bb0\u5f55\u540e\u5176\u4f59\u8bb0\u5f55\u7684\u603b\u548c\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u5229\u7528 MySQL \u8ba1\u7b97\u6570\u636e\u8868\u4e2d\u6392\u9664\u6700\u65b0\u4e00\u6761\u8bb0\u5f55\u540e\u5176\u4f59\u8bb0\u5f55\u7684\u603b\u548c\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5229\u7528  \u8ba1\u7b97\u6570\u636e\u8868\u4e2d\u6392\u9664\u6700\u65b0\u4e00\u6761\u540e\u5176\u4f59\u8bb0\u5f55\u6c42\u548c<\/strong><\/p>\n<p>\u9488\u5bf9\u7ed9\u5b9a\u7684\u6570\u636e\u8868\uff0c\u6211\u4eec\u9700\u8981\u7f16\u5199\u4e00\u4e2a mysql \u67e5\u8be2\u6765\u8ba1\u7b97\u6309\u65f6\u95f4\u6392\u5e8f\u4e14\u6309\u7c7b\u578b\u5206\u7ec4\u540e\u7684\u6570\u636e\u4e2d\uff0c\u9664\u6700\u65b0\u4e00\u6761\u8bb0\u5f55\u5916\u7684\u5176\u4f59\u6240\u6709\u8bb0\u5f55\u4e2d num \u5b57\u6bb5\u7684\u603b\u548c\u3002<\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8be2\u6765\u5b9e\u73b0\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>\u8be5\u67e5\u8be2\u4f7f\u7528\u5982\u4e0b\u6b65\u9aa4\u5b9e\u73b0\uff1a<\/p>\n<ol>\n<li>row_number() \u51fd\u6570\u7528\u4e8e\u4e3a\u6bcf\u4e2a\u7c7b\u578b\u7684\u8bb0\u5f55\u5206\u914d\u4e00\u4e2a\u6309\u65f6\u95f4\u964d\u5e8f\u7684\u5e8f\u5217\u53f7 row_num\u3002<\/li>\n<li>\u5d4c\u5957\u5b50\u67e5\u8be2\u7528\u4e8e\u6309 type \u5bf9\u8bb0\u5f55\u5206\u7ec4\uff0c\u5e76\u8ba1\u7b97\u6bcf\u4e2a\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u8bb0\u5f55\u7684 row_num\u3002<\/li>\n<li>where \u5b50\u53e5\u7b5b\u9009\u51fa row_num \u4e0d\u7b49\u4e8e 1 \u7684\u8bb0\u5f55\uff0c\u5373\u6392\u9664\u6700\u65b0\u7684\u8bb0\u5f55\u3002<\/li>\n<li>\u6700\u540e\uff0c\u6309 type \u91cd\u65b0\u5206\u7ec4\u5e76\u8ba1\u7b97 num \u5b57\u6bb5\u7684\u603b\u548c\u3002<\/li>\n<\/ol>\n<p>\u4e0a\u8ff0\u67e5\u8be2\u5c06\u8fd4\u56de\u4e00\u4e2a\u8868\uff0c\u5176\u4e2d\u5305\u542b\u6bcf\u4e2a\u7c7b\u578b\u7684 type \u548c\u9664\u6700\u65b0\u4e00\u6761\u8bb0\u5f55\u5916\u7684\u5176\u4f59\u8bb0\u5f55\u4e2d num \u5b57\u6bb5\u7684\u603b\u548c\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5229\u7528 MySQL \u8ba1\u7b97\u6570\u636e\u8868\u4e2d\u6392\u9664\u6700\u65b0\u4e00\u6761\u8bb0\u5f55\u540e\u5176\u4f59\u8bb0\u5f55\u7684\u603b\u548c\uff1f\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>\u5229\u7528 \u8ba1\u7b97\u6570\u636e\u8868\u4e2d\u6392\u9664\u6700\u65b0\u4e00\u6761\u540e\u5176\u4f59\u8bb0\u5f55\u6c42\u548c \u9488\u5bf9\u7ed9\u5b9a\u7684\u6570\u636e\u8868\uff0c\u6211\u4eec\u9700\u8981\u7f16\u5199\u4e00\u4e2a mysql \u67e5\u8be2\u6765\u8ba1\u7b97\u6309\u65f6\u95f4\u6392\u5e8f\u4e14\u6309\u7c7b\u578b\u5206\u7ec4\u540e\u7684\u6570\u636e\u4e2d\uff0c\u9664\u6700\u65b0\u4e00\u6761\u8bb0\u5f55\u5916\u7684\u5176\u4f59\u6240\u6709\u8bb0\u5f55\u4e2d num \u5b57\u6bb5\u7684\u603b\u548c\u3002 \u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8be2\u6765\u5b9e\u73b0\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 \u8be5\u67e5\u8be2\u4f7f\u7528\u5982\u4e0b\u6b65\u9aa4\u5b9e\u73b0\uff1a row_number() \u51fd\u6570\u7528\u4e8e\u4e3a\u6bcf\u4e2a\u7c7b\u578b\u7684\u8bb0\u5f55\u5206\u914d\u4e00\u4e2a\u6309\u65f6\u95f4\u964d\u5e8f\u7684\u5e8f\u5217\u53f7 row_num\u3002 \u5d4c\u5957\u5b50\u67e5\u8be2\u7528\u4e8e\u6309 type \u5bf9\u8bb0\u5f55\u5206\u7ec4\uff0c\u5e76\u8ba1\u7b97\u6bcf\u4e2a\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u8bb0\u5f55\u7684 row_num\u3002 where \u5b50\u53e5\u7b5b\u9009\u51fa row_num \u4e0d\u7b49\u4e8e 1 \u7684\u8bb0\u5f55\uff0c\u5373\u6392\u9664\u6700\u65b0\u7684\u8bb0\u5f55\u3002 \u6700\u540e\uff0c\u6309 [&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-7205","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7205","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=7205"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7205\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=7205"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=7205"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=7205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}