{"id":5920,"date":"2024-11-14T11:29:47","date_gmt":"2024-11-14T03:29:47","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5920\/"},"modified":"2024-11-14T11:29:47","modified_gmt":"2024-11-14T03:29:47","slug":"sql%e6%80%8e%e4%b9%88%e5%88%a0%e9%99%a4%e9%87%8d%e5%a4%8d%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5920\/","title":{"rendered":"sql\u600e\u4e48\u5220\u9664\u91cd\u590d\u6570\u636e"},"content":{"rendered":"<blockquote><p>\n  \u5728 sql \u4e2d\u5220\u9664\u91cd\u590d\u6570\u636e\u53ef\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\uff1a\u4f7f\u7528 distinct \u5173\u952e\u5b57\u4ece\u67e5\u8be2\u7ed3\u679c\u4e2d\u79fb\u9664\u91cd\u590d\u503c\u3002\u4f7f\u7528 distinct \u5173\u952e\u5b57\u4e0e delete \u8bed\u53e5\u4e00\u8d77\u4ece\u8868\u4e2d\u5220\u9664\u91cd\u590d\u884c\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/03\/2024060322245873294.jpg\" class=\"aligncenter\" title=\"sql\u600e\u4e48\u5220\u9664\u91cd\u590d\u6570\u636e\u63d2\u56fe\" alt=\"sql\u600e\u4e48\u5220\u9664\u91cd\u590d\u6570\u636e\u63d2\u56fe\" \/><\/p>\n<p><strong>SQL \u4e2d\u5220\u9664\u91cd\u590d\u6570\u636e<\/strong><\/p>\n<p>\u5728 SQL \u4e2d\u5220\u9664\u91cd\u590d\u6570\u636e\u7684\u4e3b\u8981\u65b9\u6cd5\u662f\u4f7f\u7528 DISTINCT \u548c DELETE \u8bed\u53e5\u3002<\/p>\n<p><strong>DISTINCT<\/strong><\/p>\n<p>DISTINCT \u5173\u952e\u5b57\u7528\u4e8e\u4ece\u67e5\u8be2\u7ed3\u679c\u4e2d\u79fb\u9664\u91cd\u590d\u503c\u3002\u5b83\u53ef\u4ee5\u4e0e SELECT \u8bed\u53e5\u4e00\u8d77\u4f7f\u7528\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>SELECT DISTINCT column_name1, column_name2\nFROM table_name;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u5c06\u8fd4\u56de\u4e00\u4e2a\u67e5\u8be2\u7ed3\u679c\uff0c\u5176\u4e2d column_name1 \u548c column_name2 \u7684\u7ec4\u5408\u662f\u552f\u4e00\u7684\u3002<\/p>\n<p><strong>DELETE<\/strong><\/p>\n<p>DELETE \u8bed\u53e5\u7528\u4e8e\u4ece\u8868\u4e2d\u5220\u9664\u6570\u636e\u3002\u8981\u5220\u9664\u91cd\u590d\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528 DISTINCT \u5173\u952e\u5b57\u4e0e DELETE \u8bed\u53e5\u4e00\u8d77\u4f7f\u7528\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>DELETE FROM table_name\nWHERE (column_name1, column_name2) IN (\n  SELECT DISTINCT column_name1, column_name2\n  FROM table_name\n);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u5c06\u5220\u9664\u8868\u4e2d\u6240\u6709\u4e0e DISTINCT \u67e5\u8be2\u8fd4\u56de\u7684\u7ed3\u679c\u5339\u914d\u7684\u91cd\u590d\u884c\u3002<\/p>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u5047\u8bbe\u6709\u4ee5\u4e0b\u8868\uff1a<\/p>\n<pre>create table my_table (\n  id int not null auto_increment primary key,\n  name varchar(255) not null,\n  email varchar(255) not null\n);\n\ninsert into my_table (name, email) values ('John Doe', 'john.doe@example.com');\ninsert into my_table (name, email) values ('John Doe', 'john.doe@example.com');\ninsert into my_table (name, email) values ('Jane Smith', 'jane.smith@example.com');\ninsert into my_table (name, email) values ('Jane Smith', 'jane.smith@example.com');<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8981\u5220\u9664\u8868\u4e2d\u91cd\u590d\u7684 name \u548c email \u7ec4\u5408\uff1a<\/p>\n<pre>DELETE FROM my_table\nWHERE (name, email) IN (\n  SELECT DISTINCT name, email\n  FROM my_table\n);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u5c06\u5220\u9664\u4e24\u4e2a John Doe \u884c\uff0c\u7559\u4e0b\u4ee5\u4e0b\u7ed3\u679c\uff1a<\/p>\n<pre>select * from my_table;\n\n| id | name | email |\n| --- | --- | --- |\n| 1 | John Doe | john.doe@example.com |\n| 3 | Jane Smith | jane.smith@example.com |<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fsql\u600e\u4e48\u5220\u9664\u91cd\u590d\u6570\u636e\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 sql \u4e2d\u5220\u9664\u91cd\u590d\u6570\u636e\u53ef\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\uff1a\u4f7f\u7528 distinct \u5173\u952e\u5b57\u4ece\u67e5\u8be2\u7ed3\u679c\u4e2d\u79fb\u9664\u91cd\u590d\u503c\u3002\u4f7f\u7528 distinct \u5173\u952e\u5b57\u4e0e delete \u8bed\u53e5\u4e00\u8d77\u4ece\u8868\u4e2d\u5220\u9664\u91cd\u590d\u884c\u3002 SQL \u4e2d\u5220\u9664\u91cd\u590d\u6570\u636e \u5728 SQL \u4e2d\u5220\u9664\u91cd\u590d\u6570\u636e\u7684\u4e3b\u8981\u65b9\u6cd5\u662f\u4f7f\u7528 DISTINCT \u548c DELETE \u8bed\u53e5\u3002 DISTINCT DISTINCT \u5173\u952e\u5b57\u7528\u4e8e\u4ece\u67e5\u8be2\u7ed3\u679c\u4e2d\u79fb\u9664\u91cd\u590d\u503c\u3002\u5b83\u53ef\u4ee5\u4e0e SELECT \u8bed\u53e5\u4e00\u8d77\u4f7f\u7528\uff0c\u5982\u4e0b\u6240\u793a\uff1a SELECT DISTINCT column_name1, column_name2 FROM table_name; \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u5c06\u8fd4\u56de\u4e00\u4e2a\u67e5\u8be2\u7ed3\u679c\uff0c\u5176\u4e2d column_name1 \u548c column_name2 \u7684\u7ec4\u5408\u662f\u552f\u4e00\u7684\u3002 DELETE DELETE \u8bed\u53e5\u7528\u4e8e\u4ece\u8868\u4e2d\u5220\u9664\u6570\u636e\u3002\u8981\u5220\u9664\u91cd\u590d\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528 DISTINCT \u5173\u952e\u5b57\u4e0e DELETE \u8bed\u53e5\u4e00\u8d77\u4f7f\u7528\uff0c\u5982\u4e0b\u6240\u793a\uff1a DELETE FROM table_name WHERE (column_name1, column_name2) IN ( SELECT DISTINCT column_name1, column_name2 FROM [&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-5920","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5920","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=5920"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5920\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5920"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5920"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5920"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}