{"id":6531,"date":"2024-11-14T10:13:02","date_gmt":"2024-11-14T02:13:02","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6531\/"},"modified":"2024-11-14T10:13:02","modified_gmt":"2024-11-14T02:13:02","slug":"mysql%e6%95%b0%e6%8d%ae%e5%ba%93%e6%80%8e%e4%b9%88%e5%88%a0%e9%99%a4%e9%87%8d%e5%a4%8d","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6531\/","title":{"rendered":"mysql\u6570\u636e\u5e93\u600e\u4e48\u5220\u9664\u91cd\u590d"},"content":{"rendered":"<blockquote><p>\n  \u5728 MySQL \u6570\u636e\u5e93\u4e2d\u5220\u9664\u91cd\u590d\u9879\u7684\u65b9\u6cd5\u6709\uff1aSELECT DISTINCT column1, column2, &#8230;FROM table_name;SELECT column1, column2, &#8230;FROM table_nameGROUP BY column1, column2, &#8230;;DELETE FROM table_nameWHERE rowid NOT IN ( SELECT MIN(rowid) FROM table_name GROUP BY column1, column\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202408\/05\/2024080519544718688.jpg\" class=\"aligncenter\" title=\"mysql\u6570\u636e\u5e93\u600e\u4e48\u5220\u9664\u91cd\u590d\u63d2\u56fe\" alt=\"mysql\u6570\u636e\u5e93\u600e\u4e48\u5220\u9664\u91cd\u590d\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5220\u9664 MySQL \u6570\u636e\u5e93\u4e2d\u7684\u91cd\u590d\u9879<\/strong><\/p>\n<p>\u5728 MySQL \u6570\u636e\u5e93\u4e2d\u5220\u9664\u91cd\u590d\u9879\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5177\u4f53\u65b9\u6cd5\u53d6\u51b3\u4e8e\u8868\u7ed3\u6784\u548c\u6570\u636e\u5206\u5e03\u3002\u4ee5\u4e0b\u662f\u5e38\u7528\u65b9\u6cd5\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 DISTINCT<\/strong><\/p>\n<pre>SELECT DISTINCT column1, column2, ...\nFROM table_name;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6b64\u65b9\u6cd5\u5c06\u8fd4\u56de\u4e0d\u5305\u542b\u91cd\u590d\u503c\u7684\u552f\u4e00\u884c\u3002\u5b83\u9002\u7528\u4e8e\u4f7f\u7528\u7d22\u5f15\u4f18\u5316\u7684\u7b80\u5355\u67e5\u8be2\u3002<\/p>\n<p><strong>2. \u4f7f\u7528 GROUP BY<\/strong><\/p>\n<pre>SELECT column1, column2, ...\nFROM table_name\nGROUP BY column1, column2, ...;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6b64\u65b9\u6cd5\u5c06\u8fd4\u56de\u6309\u6307\u5b9a\u5217\u5206\u7ec4\u7684\u552f\u4e00\u884c\u3002\u5b83\u9002\u7528\u4e8e\u5177\u6709\u591a\u4e2a\u5206\u7ec4\u5217\u7684\u590d\u6742\u67e5\u8be2\u3002<\/p>\n<p><strong>3. \u4f7f\u7528 DELETE \u8bed\u53e5<\/strong><\/p>\n<pre>DELETE FROM table_name\nWHERE rowid NOT IN (\n  SELECT MIN(rowid)\n  FROM table_name\n  GROUP BY column1, column2, ...\n);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6b64\u65b9\u6cd5\u5c06\u5220\u9664\u9664\u7b2c\u4e00\u6761\u8bb0\u5f55\u4ee5\u5916\u7684\u6240\u6709\u91cd\u590d\u8bb0\u5f55\u3002\u5b83\u9002\u7528\u4e8e\u5927\u6570\u636e\u96c6\u6216\u65e0\u6cd5\u4f7f\u7528\u7d22\u5f15\u7684\u590d\u6742\u67e5\u8be2\u3002<\/p>\n<p><strong>4. \u4f7f\u7528 UNION ALL<\/strong><\/p>\n<pre>CREATE TABLE unique_table AS\nSELECT DISTINCT column1, column2, ...\nFROM table_name;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6b64\u65b9\u6cd5\u5c06\u521b\u5efa\u4e00\u4e2a\u4e0d\u5305\u542b\u91cd\u590d\u884c\u7684\u8f85\u52a9\u8868\u3002\u7136\u540e\u53ef\u4ee5\u5220\u9664\u539f\u59cb\u8868\u5e76\u4f7f\u7528\u8f85\u52a9\u8868\u91cd\u65b0\u586b\u5145\u5b83\u3002<\/p>\n<p><strong>\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5<\/strong><\/p>\n<p>\u6700\u4f73\u65b9\u6cd5\u53d6\u51b3\u4e8e\u4ee5\u4e0b\u56e0\u7d20\uff1a<\/p>\n<ul>\n<li>\u6570\u636e\u91cf<\/li>\n<li>\u8868\u7ed3\u6784<\/li>\n<li>\u67e5\u8be2\u590d\u6742\u6027<\/li>\n<li>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f15<\/li>\n<\/ul>\n<p>\u8bf7\u4ed4\u7ec6\u8bc4\u4f30\u8fd9\u4e9b\u56e0\u7d20\u4ee5\u9009\u62e9\u6700\u6709\u6548\u7684\u65b9\u6cd5\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u6570\u636e\u5e93\u600e\u4e48\u5220\u9664\u91cd\u590d\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>\u5728 MySQL \u6570\u636e\u5e93\u4e2d\u5220\u9664\u91cd\u590d\u9879\u7684\u65b9\u6cd5\u6709\uff1aSELECT DISTINCT column1, column2, &#8230;FROM table_name;SELECT column1, column2, &#8230;FROM table_nameGROUP BY column1, column2, &#8230;;DELETE FROM table_nameWHERE rowid NOT IN ( SELECT MIN(rowid) FROM table_name GROUP BY column1, column \u5982\u4f55\u5220\u9664 MySQL \u6570\u636e\u5e93\u4e2d\u7684\u91cd\u590d\u9879 \u5728 MySQL \u6570\u636e\u5e93\u4e2d\u5220\u9664\u91cd\u590d\u9879\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5177\u4f53\u65b9\u6cd5\u53d6\u51b3\u4e8e\u8868\u7ed3\u6784\u548c\u6570\u636e\u5206\u5e03\u3002\u4ee5\u4e0b\u662f\u5e38\u7528\u65b9\u6cd5\uff1a 1. \u4f7f\u7528 DISTINCT SELECT DISTINCT column1, column2, &#8230; FROM table_name; \u767b\u5f55\u540e\u590d\u5236 \u6b64\u65b9\u6cd5\u5c06\u8fd4\u56de\u4e0d\u5305\u542b\u91cd\u590d\u503c\u7684\u552f\u4e00\u884c\u3002\u5b83\u9002\u7528\u4e8e\u4f7f\u7528\u7d22\u5f15\u4f18\u5316\u7684\u7b80\u5355\u67e5\u8be2\u3002 2. \u4f7f\u7528 GROUP BY SELECT column1, column2, [&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-6531","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6531","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=6531"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6531\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6531"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6531"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6531"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}