{"id":7287,"date":"2024-11-14T13:44:12","date_gmt":"2024-11-14T05:44:12","guid":{"rendered":"https:\/\/fwq.ai\/blog\/7287\/"},"modified":"2024-11-14T13:44:12","modified_gmt":"2024-11-14T05:44:12","slug":"mysql-%e5%ad%90%e6%9f%a5%e8%af%a2%e6%8e%92%e5%ba%8f%e7%bb%93%e6%9e%9c%e4%b8%8d%e4%bf%9d%e7%95%99%ef%bc%9f%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96%e6%af%8f%e4%b8%aa%e7%94%a8%e6%88%b7%e6%9c%80%e6%96%b0","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/7287\/","title":{"rendered":"MySQL \u5b50\u67e5\u8be2\u6392\u5e8f\u7ed3\u679c\u4e0d\u4fdd\u7559\uff1f\u5982\u4f55\u83b7\u53d6\u6bcf\u4e2a\u7528\u6237\u6700\u65b0\u4ea7\u54c1\u8bb0\u5f55\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173068111736371.jpg\" class=\"aligncenter\" title=\"MySQL \u5b50\u67e5\u8be2\u6392\u5e8f\u7ed3\u679c\u4e0d\u4fdd\u7559\uff1f\u5982\u4f55\u83b7\u53d6\u6bcf\u4e2a\u7528\u6237\u6700\u65b0\u4ea7\u54c1\u8bb0\u5f55\uff1f\u63d2\u56fe\" alt=\"MySQL \u5b50\u67e5\u8be2\u6392\u5e8f\u7ed3\u679c\u4e0d\u4fdd\u7559\uff1f\u5982\u4f55\u83b7\u53d6\u6bcf\u4e2a\u7528\u6237\u6700\u65b0\u4ea7\u54c1\u8bb0\u5f55\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5b50\u67e5\u8be2\u6392\u5e8f\u4e0d\u4fdd\u7559\uff1f\u6559\u4f60\u4e00\u62db\u89e3\u51b3\uff01<\/strong><\/p>\n<p><strong>\u95ee\u9898\u8be6\u60c5\uff1a<\/strong><\/p>\n<p>\u5728  5.7.13 \u4e2d\uff0c\u4e00\u4e2a\u7528\u6237\u6709\u4e00\u4e2a\u4ea7\u54c1\uff0c\u53ea\u663e\u793a\u6700\u65b0\u7684\u8bb0\u5f55\uff0c\u4f46\u5b50\u67e5\u8be2\u6392\u5e8f\u540e\uff0c\u6392\u5e8f\u7ed3\u679c\u4e0d\u4fdd\u7559\u3002\u5c1d\u8bd5\u4e86\u5206\u7ec4\u540e\u518d\u6392\u5e8f\uff0c\u4f46\u4ecd\u7136\u65e0\u6cd5\u6ee1\u8db3\u9700\u6c42\u3002\u5bfb\u6c42\u66f4\u9760\u8c31\u3001\u66f4\u7b80\u5355\u7684\u5199\u6cd5\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong><\/p>\n<p>\u7531\u4e8e mysql 5.7 \u7248\u672c\u4e0d\u652f\u6301\u7a97\u53e3\u51fd\u6570\uff0c\u56e0\u6b64\u65e0\u6cd5\u4f7f\u7528\u7a97\u53e3\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u4e0d\u8fc7\uff0c\u8fd9\u91cc\u63d0\u4f9b\u4e00\u79cd\u601d\u8def\uff1a<\/p>\n<p><strong>1. \u8ba1\u7b97\u6bcf\u4e2a\u5206\u7ec4\u7684\u6700\u5927\u521b\u5efa\u65f6\u95f4<\/strong><\/p>\n<pre>select max(create_time) as create_time, user_id, product_id\nfrom demo\ngroup by user_id, product_id<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u5173\u8054\u539f\u8868\u627e\u5230\u5bf9\u5e94\u8bb0\u5f55<\/strong><\/p>\n<p>\u5c06\u4e0a\u4e00\u6b65\u5f97\u5230\u7684\u6700\u5927\u521b\u5efa\u65f6\u95f4\u4e0e\u539f\u8868\u5173\u8054\uff0c\u627e\u5230\u6bcf\u4e2a\u5206\u7ec4\u4e2d\u6700\u65b0\u7684\u8bb0\u5f55\u3002<\/p>\n<pre>select t2.id, t1.* from (\n    select max(create_time) as create_time, user_id, product_id\n    from demo\n    group by user_id, product_id\n) t1\nleft join demo t2\non t1.user_id = t2.user_id\nand t1.product_id = t2.product_id\nand t1.create_time = t2.create_time<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fMySQL \u5b50\u67e5\u8be2\u6392\u5e8f\u7ed3\u679c\u4e0d\u4fdd\u7559\uff1f\u5982\u4f55\u83b7\u53d6\u6bcf\u4e2a\u7528\u6237\u6700\u65b0\u4ea7\u54c1\u8bb0\u5f55\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>\u5b50\u67e5\u8be2\u6392\u5e8f\u4e0d\u4fdd\u7559\uff1f\u6559\u4f60\u4e00\u62db\u89e3\u51b3\uff01 \u95ee\u9898\u8be6\u60c5\uff1a \u5728 5.7.13 \u4e2d\uff0c\u4e00\u4e2a\u7528\u6237\u6709\u4e00\u4e2a\u4ea7\u54c1\uff0c\u53ea\u663e\u793a\u6700\u65b0\u7684\u8bb0\u5f55\uff0c\u4f46\u5b50\u67e5\u8be2\u6392\u5e8f\u540e\uff0c\u6392\u5e8f\u7ed3\u679c\u4e0d\u4fdd\u7559\u3002\u5c1d\u8bd5\u4e86\u5206\u7ec4\u540e\u518d\u6392\u5e8f\uff0c\u4f46\u4ecd\u7136\u65e0\u6cd5\u6ee1\u8db3\u9700\u6c42\u3002\u5bfb\u6c42\u66f4\u9760\u8c31\u3001\u66f4\u7b80\u5355\u7684\u5199\u6cd5\u3002 \u89e3\u51b3\u65b9\u6848\uff1a \u7531\u4e8e mysql 5.7 \u7248\u672c\u4e0d\u652f\u6301\u7a97\u53e3\u51fd\u6570\uff0c\u56e0\u6b64\u65e0\u6cd5\u4f7f\u7528\u7a97\u53e3\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u4e0d\u8fc7\uff0c\u8fd9\u91cc\u63d0\u4f9b\u4e00\u79cd\u601d\u8def\uff1a 1. \u8ba1\u7b97\u6bcf\u4e2a\u5206\u7ec4\u7684\u6700\u5927\u521b\u5efa\u65f6\u95f4 select max(create_time) as create_time, user_id, product_id from demo group by user_id, product_id \u767b\u5f55\u540e\u590d\u5236 2. \u5173\u8054\u539f\u8868\u627e\u5230\u5bf9\u5e94\u8bb0\u5f55 \u5c06\u4e0a\u4e00\u6b65\u5f97\u5230\u7684\u6700\u5927\u521b\u5efa\u65f6\u95f4\u4e0e\u539f\u8868\u5173\u8054\uff0c\u627e\u5230\u6bcf\u4e2a\u5206\u7ec4\u4e2d\u6700\u65b0\u7684\u8bb0\u5f55\u3002 select t2.id, t1.* from ( select max(create_time) as create_time, user_id, product_id from demo group by user_id, product_id ) t1 left join demo t2 on t1.user_id = t2.user_id [&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-7287","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7287","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=7287"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7287\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=7287"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=7287"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=7287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}