{"id":5536,"date":"2024-11-14T13:30:54","date_gmt":"2024-11-14T05:30:54","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5536\/"},"modified":"2024-11-14T13:30:54","modified_gmt":"2024-11-14T05:30:54","slug":"mysql%e6%b6%88%e9%99%a4%e9%87%8d%e5%a4%8d%e8%a1%8c%e6%80%8e%e4%b9%88%e5%8a%9e","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5536\/","title":{"rendered":"mysql\u6d88\u9664\u91cd\u590d\u884c\u600e\u4e48\u529e"},"content":{"rendered":"<blockquote><p>\n  \u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u6d88\u9664 mysql \u4e2d\u7684\u91cd\u590d\u884c\uff1a\u4f7f\u7528 unique \u6216 primary key \u7ea6\u675f\u5f3a\u5236\u552f\u4e00\u6027\u3002\u4f7f\u7528 distinct \u5173\u952e\u5b57\u8fc7\u6ee4\u91cd\u590d\u884c\u3002\u4f7f\u7528 group by \u5b50\u53e5\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u7ec4\u3002\u4f7f\u7528 delete \u8bed\u53e5\u5220\u9664\u91cd\u590d\u884c\u3002\u4f7f\u7528\u7b2c\u4e09\u65b9\u5de5\u5177\u6216\u6269\u5c55\u6765\u7b80\u5316\u6b64\u8fc7\u7a0b\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/29\/2024052908332445662.jpg\" class=\"aligncenter\" title=\"mysql\u6d88\u9664\u91cd\u590d\u884c\u600e\u4e48\u529e\u63d2\u56fe\" alt=\"mysql\u6d88\u9664\u91cd\u590d\u884c\u600e\u4e48\u529e\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 MySQL \u4e2d\u6d88\u9664\u91cd\u590d\u884c<\/strong><\/p>\n<p>\u91cd\u590d\u884c\u5728 MySQL \u6570\u636e\u5e93\u4e2d\u5f88\u5e38\u89c1\uff0c\u5b83\u4eec\u53ef\u80fd\u5bfc\u81f4\u6570\u636e\u4e0d\u4e00\u81f4\u548c\u67e5\u8be2\u6027\u80fd\u964d\u4f4e\u3002\u6d88\u9664\u91cd\u590d\u884c\u81f3\u5173\u91cd\u8981\uff0c\u4ee5\u786e\u4fdd\u6570\u636e\u51c6\u786e\u6027\u548c\u4f18\u5316\u67e5\u8be2\u3002<\/p>\n<p><strong>\u65b9\u6cd5 1\uff1a\u4f7f\u7528 UNIQUE \u6216 PRIMARY KEY \u7ea6\u675f<\/strong><\/p>\n<p>\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4e3a\u8868\u4e2d\u8981\u552f\u4e00\u786e\u5b9a\u7684\u5217\u8bbe\u7f6e UNIQUE \u6216 PRIMARY KEY \u7ea6\u675f\u3002\u8fd9\u4e9b\u7ea6\u675f\u5f3a\u5236\u552f\u4e00\u6027\uff0c\u9632\u6b62\u63d2\u5165\u91cd\u590d\u503c\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>CREATE TABLE mytable (\n  id INT NOT NULL AUTO_INCREMENT,\n  name VARCHAR(255) UNIQUE\n);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u65b9\u6cd5 2\uff1a\u4f7f\u7528 DISTINCT \u5173\u952e\u5b57<\/strong><\/p>\n<p>DISTINCT \u5173\u952e\u5b57\u53ef\u7528\u4e8e\u4ece\u67e5\u8be2\u7ed3\u679c\u4e2d\u6d88\u9664\u91cd\u590d\u884c\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>SELECT DISTINCT name FROM mytable;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u65b9\u6cd5 3\uff1a\u4f7f\u7528 GROUP BY \u5b50\u53e5<\/strong><\/p>\n<p>GROUP BY \u5b50\u53e5\u53ef\u7528\u4e8e\u5c06\u8868\u4e2d\u7684\u6570\u636e\u5206\u7ec4\uff0c\u5e76\u4ec5\u8fd4\u56de\u6bcf\u4e2a\u7ec4\u7684\u552f\u4e00\u884c\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>SELECT name FROM mytable\nGROUP BY name;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u65b9\u6cd5 4\uff1a\u4f7f\u7528 DELETE \u8bed\u53e5<\/strong><\/p>\n<p>DELETE \u8bed\u53e5\u53ef\u7528\u4e8e\u5220\u9664\u91cd\u590d\u884c\u3002\u786e\u4fdd\u9884\u5148\u4f7f\u7528 DISTINCT \u6216 GROUP BY \u786e\u5b9a\u552f\u4e00\u884c\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>DELETE FROM mytable\nWHERE id NOT IN (SELECT DISTINCT id FROM mytable);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u65b9\u6cd5 5\uff1a\u4f7f\u7528\u7b2c\u4e09\u65b9\u5de5\u5177<\/strong><\/p>\n<p>\u6709\u8bb8\u591a\u7b2c\u4e09\u65b9\u5de5\u5177\u548c\u6269\u5c55\u53ef\u4ee5\u5e2e\u52a9\u6d88\u9664\u91cd\u590d\u884c\u3002\u8bf7\u53c2\u9605 MySQL \u6587\u6863\u4ee5\u83b7\u53d6\u66f4\u591a\u4fe1\u606f\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u6d88\u9664\u91cd\u590d\u884c\u600e\u4e48\u529e\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>\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u6d88\u9664 mysql \u4e2d\u7684\u91cd\u590d\u884c\uff1a\u4f7f\u7528 unique \u6216 primary key \u7ea6\u675f\u5f3a\u5236\u552f\u4e00\u6027\u3002\u4f7f\u7528 distinct \u5173\u952e\u5b57\u8fc7\u6ee4\u91cd\u590d\u884c\u3002\u4f7f\u7528 group by \u5b50\u53e5\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u7ec4\u3002\u4f7f\u7528 delete \u8bed\u53e5\u5220\u9664\u91cd\u590d\u884c\u3002\u4f7f\u7528\u7b2c\u4e09\u65b9\u5de5\u5177\u6216\u6269\u5c55\u6765\u7b80\u5316\u6b64\u8fc7\u7a0b\u3002 \u5982\u4f55\u5728 MySQL \u4e2d\u6d88\u9664\u91cd\u590d\u884c \u91cd\u590d\u884c\u5728 MySQL \u6570\u636e\u5e93\u4e2d\u5f88\u5e38\u89c1\uff0c\u5b83\u4eec\u53ef\u80fd\u5bfc\u81f4\u6570\u636e\u4e0d\u4e00\u81f4\u548c\u67e5\u8be2\u6027\u80fd\u964d\u4f4e\u3002\u6d88\u9664\u91cd\u590d\u884c\u81f3\u5173\u91cd\u8981\uff0c\u4ee5\u786e\u4fdd\u6570\u636e\u51c6\u786e\u6027\u548c\u4f18\u5316\u67e5\u8be2\u3002 \u65b9\u6cd5 1\uff1a\u4f7f\u7528 UNIQUE \u6216 PRIMARY KEY \u7ea6\u675f \u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4e3a\u8868\u4e2d\u8981\u552f\u4e00\u786e\u5b9a\u7684\u5217\u8bbe\u7f6e UNIQUE \u6216 PRIMARY KEY \u7ea6\u675f\u3002\u8fd9\u4e9b\u7ea6\u675f\u5f3a\u5236\u552f\u4e00\u6027\uff0c\u9632\u6b62\u63d2\u5165\u91cd\u590d\u503c\u3002 \u793a\u4f8b\uff1a CREATE TABLE mytable ( id INT NOT NULL AUTO_INCREMENT, name VARCHAR(255) UNIQUE ); \u767b\u5f55\u540e\u590d\u5236 \u65b9\u6cd5 2\uff1a\u4f7f\u7528 DISTINCT \u5173\u952e\u5b57 DISTINCT \u5173\u952e\u5b57\u53ef\u7528\u4e8e\u4ece\u67e5\u8be2\u7ed3\u679c\u4e2d\u6d88\u9664\u91cd\u590d\u884c\u3002 [&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-5536","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5536","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=5536"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5536\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5536"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5536"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5536"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}