{"id":6507,"date":"2024-11-14T08:19:00","date_gmt":"2024-11-14T00:19:00","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6507\/"},"modified":"2024-11-14T08:19:00","modified_gmt":"2024-11-14T00:19:00","slug":"%e6%95%b0%e6%8d%ae%e5%ba%93mysql%e6%80%8e%e4%b9%88%e5%88%a0%e9%99%a4%e9%87%8d%e5%a4%8d%e9%a1%b9","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6507\/","title":{"rendered":"\u6570\u636e\u5e93mysql\u600e\u4e48\u5220\u9664\u91cd\u590d\u9879"},"content":{"rendered":"<blockquote><p>\n  \u8981\u4ece mysql \u6570\u636e\u5e93\u4e2d\u5220\u9664\u91cd\u590d\u9879\uff0c\u53ef\u4ee5\u91c7\u53d6\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u786e\u5b9a\u8981\u5220\u9664\u91cd\u590d\u9879\u7684\u5217\u6216\u7ec4\u5408\u5217\u3002\u4f7f\u7528 distinct \u6216 group by \u8bed\u53e5\u5220\u9664\u91cd\u590d\u884c\u3002\u4f7f\u7528 delete with \u5173\u952e\u5b57\u6c38\u4e45\u5220\u9664\u91cd\u590d\u884c\uff0c\u786e\u4fdd\u6309\u4e3b\u952e\u6392\u5e8f\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202408\/05\/2024080517514254213.jpg\" class=\"aligncenter\" title=\"\u6570\u636e\u5e93mysql\u600e\u4e48\u5220\u9664\u91cd\u590d\u9879\u63d2\u56fe\" alt=\"\u6570\u636e\u5e93mysql\u600e\u4e48\u5220\u9664\u91cd\u590d\u9879\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4ece MySQL \u6570\u636e\u5e93\u4e2d\u5220\u9664\u91cd\u590d\u9879<\/strong><\/p>\n<p><strong>\u65b9\u6cd5\uff1a<\/strong><\/p>\n<p><strong>1. \u786e\u5b9a\u8981\u5220\u9664\u91cd\u590d\u9879\u7684\u5217\u6216\u7ec4\u5408\u5217<\/strong><\/p>\n<p>\u9996\u5148\uff0c\u786e\u5b9a\u9700\u8981\u5220\u9664\u91cd\u590d\u9879\u7684\u5217\u6216\u7ec4\u5408\u5217\u3002\u8fd9\u53ef\u4ee5\u6839\u636e\u4e1a\u52a1\u9700\u6c42\u6216\u6570\u636e\u4e00\u81f4\u6027\u8981\u6c42\u6765\u786e\u5b9a\u3002\u4f8b\u5982\uff0c\u5982\u679c\u9700\u8981\u5220\u9664\u5ba2\u6237\u8868\u4e2d\u91cd\u590d\u7684\u7535\u5b50\u90ae\u4ef6\u5730\u5740\uff0c\u5219\u9700\u8981\u5c06\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u4f5c\u4e3a\u76ee\u6807\u5217\u3002<\/p>\n<p><strong>2. \u4f7f\u7528 DISTINCT \u6216 GROUP BY \u8bed\u53e5<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4e0d\u4f7f\u7528\u989d\u5916\u6b65\u9aa4\u5220\u9664\u91cd\u590d\u9879\u7684\u4e24\u79cd\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li><strong>DISTINCT \u8bed\u53e5\uff1a<\/strong><\/li>\n<\/ul>\n<pre>SELECT DISTINCT &lt;\u76ee\u6807\u5217&gt; FROM &lt;table&gt;;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li><strong>GROUP BY \u8bed\u53e5\uff1a<\/strong><\/li>\n<\/ul>\n<pre>SELECT &lt;\u76ee\u6807\u5217&gt; FROM &lt;table&gt; GROUP BY &lt;\u76ee\u6807\u5217&gt;;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528 DELETE WITH \u5173\u952e\u5b57<\/strong><\/p>\n<p>\u5982\u679c\u9700\u8981\u4ece\u8868\u4e2d\u6c38\u4e45\u5220\u9664\u91cd\u590d\u9879\uff0c\u53ef\u4ee5\u4f7f\u7528 DELETE WITH \u5173\u952e\u5b57\uff1a<\/p>\n<pre>DELETE t1 FROM &lt;table&gt; t1, &lt;table&gt; t2\nWHERE t1.&lt;\u76ee\u6807\u5217&gt; = t2.&lt;\u76ee\u6807\u5217&gt;\nAND t1.&lt;\u4e3b\u952e\u5217&gt; &gt; t2.&lt;\u4e3b\u952e\u5217&gt;;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li>t1 \u548c t2 \u662f\u540c\u4e00\u5f20\u8868\u7684\u4e24\u4e2a\u522b\u540d\u3002<\/li>\n<li> \u662f\u8868\u7684\u552f\u4e00\u6807\u8bc6\u7b26\u5217\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u4e3a\u4e86\u4ece\u5ba2\u6237\u8868\u4e2d\u5220\u9664\u91cd\u590d\u7684\u7535\u5b50\u90ae\u4ef6\u5730\u5740\uff1a<\/p>\n<pre>DELETE t1 FROM customers t1, customers t2\nWHERE t1.email = t2.email\nAND t1.customer_id &gt; t2.customer_id;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<ul>\n<li>\u5220\u9664\u91cd\u590d\u9879\u53ef\u80fd\u4f1a\u5f71\u54cd\u8868\u4e2d\u5176\u4ed6\u6570\u636e\u7684\u5b8c\u6574\u6027\uff0c\u56e0\u6b64\u5728\u6267\u884c\u6b64\u64cd\u4f5c\u4e4b\u524d\u8bf7\u786e\u4fdd\u60a8\u4e86\u89e3\u6f5c\u5728\u7684\u540e\u679c\u3002<\/li>\n<li>\u5bf9\u4e8e\u5927\u578b\u6570\u636e\u96c6\uff0c\u5220\u9664\u91cd\u590d\u9879\u53ef\u80fd\u9700\u8981\u5f88\u957f\u65f6\u95f4\u3002\u5efa\u8bae\u5728\u975e\u9ad8\u5cf0\u65f6\u6bb5\u6216\u4f7f\u7528\u5206\u5757\u5220\u9664\u6280\u672f\u6267\u884c\u6b64\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u6570\u636e\u5e93\u600e\u4e48\u5220\u9664\u91cd\u590d\u9879\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>\u8981\u4ece mysql \u6570\u636e\u5e93\u4e2d\u5220\u9664\u91cd\u590d\u9879\uff0c\u53ef\u4ee5\u91c7\u53d6\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u786e\u5b9a\u8981\u5220\u9664\u91cd\u590d\u9879\u7684\u5217\u6216\u7ec4\u5408\u5217\u3002\u4f7f\u7528 distinct \u6216 group by \u8bed\u53e5\u5220\u9664\u91cd\u590d\u884c\u3002\u4f7f\u7528 delete with \u5173\u952e\u5b57\u6c38\u4e45\u5220\u9664\u91cd\u590d\u884c\uff0c\u786e\u4fdd\u6309\u4e3b\u952e\u6392\u5e8f\u3002 \u5982\u4f55\u4ece MySQL \u6570\u636e\u5e93\u4e2d\u5220\u9664\u91cd\u590d\u9879 \u65b9\u6cd5\uff1a 1. \u786e\u5b9a\u8981\u5220\u9664\u91cd\u590d\u9879\u7684\u5217\u6216\u7ec4\u5408\u5217 \u9996\u5148\uff0c\u786e\u5b9a\u9700\u8981\u5220\u9664\u91cd\u590d\u9879\u7684\u5217\u6216\u7ec4\u5408\u5217\u3002\u8fd9\u53ef\u4ee5\u6839\u636e\u4e1a\u52a1\u9700\u6c42\u6216\u6570\u636e\u4e00\u81f4\u6027\u8981\u6c42\u6765\u786e\u5b9a\u3002\u4f8b\u5982\uff0c\u5982\u679c\u9700\u8981\u5220\u9664\u5ba2\u6237\u8868\u4e2d\u91cd\u590d\u7684\u7535\u5b50\u90ae\u4ef6\u5730\u5740\uff0c\u5219\u9700\u8981\u5c06\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u4f5c\u4e3a\u76ee\u6807\u5217\u3002 2. \u4f7f\u7528 DISTINCT \u6216 GROUP BY \u8bed\u53e5 \u4ee5\u4e0b\u662f\u4e0d\u4f7f\u7528\u989d\u5916\u6b65\u9aa4\u5220\u9664\u91cd\u590d\u9879\u7684\u4e24\u79cd\u65b9\u6cd5\uff1a DISTINCT \u8bed\u53e5\uff1a SELECT DISTINCT &lt;\u76ee\u6807\u5217&gt; FROM &lt;table&gt;; \u767b\u5f55\u540e\u590d\u5236 GROUP BY \u8bed\u53e5\uff1a SELECT &lt;\u76ee\u6807\u5217&gt; FROM &lt;table&gt; GROUP BY &lt;\u76ee\u6807\u5217&gt;; \u767b\u5f55\u540e\u590d\u5236 3. \u4f7f\u7528 DELETE WITH \u5173\u952e\u5b57 \u5982\u679c\u9700\u8981\u4ece\u8868\u4e2d\u6c38\u4e45\u5220\u9664\u91cd\u590d\u9879\uff0c\u53ef\u4ee5\u4f7f\u7528 DELETE WITH \u5173\u952e\u5b57\uff1a DELETE [&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-6507","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6507","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=6507"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6507\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6507"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6507"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6507"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}