{"id":5686,"date":"2024-11-14T10:53:32","date_gmt":"2024-11-14T02:53:32","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5686\/"},"modified":"2024-11-14T10:53:32","modified_gmt":"2024-11-14T02:53:32","slug":"sql%e5%8e%bb%e9%87%8d%e5%88%a0%e9%99%a4%e8%af%ad%e5%8f%a5%e6%80%8e%e4%b9%88%e5%86%99","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5686\/","title":{"rendered":"sql\u53bb\u91cd\u5220\u9664\u8bed\u53e5\u600e\u4e48\u5199"},"content":{"rendered":"<blockquote><p>\n  sql \u4e2d\u7684\u53bb\u91cd\u5220\u9664\u8bed\u53e5\u53ef\u4ece\u8868\u4e2d\u5220\u9664\u91cd\u590d\u884c\uff0c\u4fdd\u7559\u552f\u4e00\u503c\u3002\u6b65\u9aa4\uff1a\u786e\u5b9a\u8981\u5220\u9664\u91cd\u590d\u884c\u7684\u5217\uff1b\u5728 delete \u8bed\u53e5\u4e2d\u4f7f\u7528 distinct \u5173\u952e\u5b57\uff1b\u6307\u5b9a\u8981\u5220\u9664\u7684\u5217\uff1b\u6307\u5b9a\u8981\u5220\u9664\u884c\u7684\u8868\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/30\/2024053020421950556.jpg\" class=\"aligncenter\" title=\"sql\u53bb\u91cd\u5220\u9664\u8bed\u53e5\u600e\u4e48\u5199\u63d2\u56fe\" alt=\"sql\u53bb\u91cd\u5220\u9664\u8bed\u53e5\u600e\u4e48\u5199\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u7f16\u5199 SQL \u53bb\u91cd\u5220\u9664\u8bed\u53e5<\/strong><\/p>\n<p>SQL \u4e2d\u7684\u53bb\u91cd\u5220\u9664\u8bed\u53e5\u7528\u4e8e\u4ece\u8868\u4e2d\u5220\u9664\u91cd\u590d\u7684\u884c\uff0c\u4fdd\u7559\u552f\u4e00\u503c\u3002\u4ee5\u4e0b\u6b65\u9aa4\u6307\u5357\u5c06\u6f14\u793a\u5982\u4f55\u7f16\u5199\u6b64\u7c7b\u8bed\u53e5\uff1a<\/p>\n<p><strong>1. \u786e\u5b9a\u8981\u5220\u9664\u91cd\u590d\u884c\u7684\u5217<\/strong><\/p>\n<p>\u9996\u5148\uff0c\u786e\u5b9a\u8981\u7528\u6765\u8bc6\u522b\u548c\u5220\u9664\u91cd\u590d\u884c\u7684\u5217\u3002\u8fd9\u4e9b\u5217\u5305\u542b\u552f\u4e00\u6807\u8bc6\u7b26\u6216\u5176\u4ed6\u53ef\u4ee5\u5c06\u552f\u4e00\u884c\u533a\u5206\u5f00\u6765\u7684\u503c\u3002<\/p>\n<p><strong>2. \u4f7f\u7528 DISTINCT \u5173\u952e\u5b57<\/strong><\/p>\n<p>\u5728 DELETE \u8bed\u53e5\u4e2d\u4f7f\u7528 DISTINCT \u5173\u952e\u5b57\u6765\u6307\u5b9a\u8981\u5220\u9664\u7684\u91cd\u590d\u884c\u3002DISTINCT \u5c06\u786e\u4fdd\u53ea\u5220\u9664\u5305\u542b\u91cd\u590d\u503c\u7684\u884c\u7684\u6700\u5c0f\u96c6\u5408\u3002<\/p>\n<p><strong>3. \u6307\u5b9a\u8981\u5220\u9664\u7684\u5217<\/strong><\/p>\n<p>\u5728 DISTINCT \u4e4b\u540e\uff0c\u6307\u5b9a\u8981\u7528\u6765\u67e5\u627e\u548c\u5220\u9664\u91cd\u590d\u884c\u7684\u5217\u3002\u53ef\u4ee5\u6307\u5b9a\u591a\u4e2a\u5217\uff0c\u7528\u9017\u53f7\u5206\u9694\u3002<\/p>\n<p><strong>4. \u6307\u5b9a\u8981\u5220\u9664\u884c\u7684\u8868<\/strong><\/p>\n<p>\u5728 WHERE \u5b50\u53e5\u4e2d\u6307\u5b9a\u8981\u4ece\u4e2d\u5220\u9664\u91cd\u590d\u884c\u7684\u8868\u3002<\/p>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b SQL \u8bed\u53e5\uff0c\u5b83\u5c06\u4ece &#8220;students&#8221; \u8868\u4e2d\u5220\u9664\u5177\u6709\u91cd\u590d &#8220;student_id&#8221; \u503c\u7684\u884c\uff0c\u4fdd\u7559\u552f\u4e00\u503c\uff1a<\/p>\n<pre>DELETE FROM students\nWHERE student_id IN (SELECT DISTINCT student_id FROM students);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6267\u884c\u8bed\u53e5<\/strong><\/p>\n<p>\u6267\u884c SQL \u53bb\u91cd\u5220\u9664\u8bed\u53e5\u540e\uff0c\u5b83\u5c06\u4ec5\u5220\u9664\u5305\u542b\u91cd\u590d\u503c\u7684\u884c\u7684\u6700\u5c0f\u96c6\u5408\uff0c\u4fdd\u7559\u552f\u4e00\u503c\u3002<\/p>\n<p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<ul>\n<li>\u786e\u4fdd\u4f60\u5177\u6709\u5bf9\u8868\u7684\u9002\u5f53\u6743\u9650\u624d\u80fd\u6267\u884c DELETE \u64cd\u4f5c\u3002<\/li>\n<li>\u5728\u6267\u884c\u5220\u9664\u8bed\u53e5\u4e4b\u524d\uff0c\u52a1\u5fc5\u5907\u4efd\u6570\u636e\uff0c\u4ee5\u9632\u610f\u5916\u3002<\/li>\n<li>\u5bf9\u4e8e\u5927\u8868\uff0c\u53bb\u91cd\u5220\u9664\u64cd\u4f5c\u53ef\u80fd\u4f1a\u9700\u8981\u5927\u91cf\u65f6\u95f4\u548c\u8d44\u6e90\uff0c\u56e0\u6b64\u8bf7\u8c28\u614e\u4f7f\u7528\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fsql\u53bb\u91cd\u5220\u9664\u8bed\u53e5\u600e\u4e48\u5199\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>sql \u4e2d\u7684\u53bb\u91cd\u5220\u9664\u8bed\u53e5\u53ef\u4ece\u8868\u4e2d\u5220\u9664\u91cd\u590d\u884c\uff0c\u4fdd\u7559\u552f\u4e00\u503c\u3002\u6b65\u9aa4\uff1a\u786e\u5b9a\u8981\u5220\u9664\u91cd\u590d\u884c\u7684\u5217\uff1b\u5728 delete \u8bed\u53e5\u4e2d\u4f7f\u7528 distinct \u5173\u952e\u5b57\uff1b\u6307\u5b9a\u8981\u5220\u9664\u7684\u5217\uff1b\u6307\u5b9a\u8981\u5220\u9664\u884c\u7684\u8868\u3002 \u5982\u4f55\u7f16\u5199 SQL \u53bb\u91cd\u5220\u9664\u8bed\u53e5 SQL \u4e2d\u7684\u53bb\u91cd\u5220\u9664\u8bed\u53e5\u7528\u4e8e\u4ece\u8868\u4e2d\u5220\u9664\u91cd\u590d\u7684\u884c\uff0c\u4fdd\u7559\u552f\u4e00\u503c\u3002\u4ee5\u4e0b\u6b65\u9aa4\u6307\u5357\u5c06\u6f14\u793a\u5982\u4f55\u7f16\u5199\u6b64\u7c7b\u8bed\u53e5\uff1a 1. \u786e\u5b9a\u8981\u5220\u9664\u91cd\u590d\u884c\u7684\u5217 \u9996\u5148\uff0c\u786e\u5b9a\u8981\u7528\u6765\u8bc6\u522b\u548c\u5220\u9664\u91cd\u590d\u884c\u7684\u5217\u3002\u8fd9\u4e9b\u5217\u5305\u542b\u552f\u4e00\u6807\u8bc6\u7b26\u6216\u5176\u4ed6\u53ef\u4ee5\u5c06\u552f\u4e00\u884c\u533a\u5206\u5f00\u6765\u7684\u503c\u3002 2. \u4f7f\u7528 DISTINCT \u5173\u952e\u5b57 \u5728 DELETE \u8bed\u53e5\u4e2d\u4f7f\u7528 DISTINCT \u5173\u952e\u5b57\u6765\u6307\u5b9a\u8981\u5220\u9664\u7684\u91cd\u590d\u884c\u3002DISTINCT \u5c06\u786e\u4fdd\u53ea\u5220\u9664\u5305\u542b\u91cd\u590d\u503c\u7684\u884c\u7684\u6700\u5c0f\u96c6\u5408\u3002 3. \u6307\u5b9a\u8981\u5220\u9664\u7684\u5217 \u5728 DISTINCT \u4e4b\u540e\uff0c\u6307\u5b9a\u8981\u7528\u6765\u67e5\u627e\u548c\u5220\u9664\u91cd\u590d\u884c\u7684\u5217\u3002\u53ef\u4ee5\u6307\u5b9a\u591a\u4e2a\u5217\uff0c\u7528\u9017\u53f7\u5206\u9694\u3002 4. \u6307\u5b9a\u8981\u5220\u9664\u884c\u7684\u8868 \u5728 WHERE \u5b50\u53e5\u4e2d\u6307\u5b9a\u8981\u4ece\u4e2d\u5220\u9664\u91cd\u590d\u884c\u7684\u8868\u3002 \u793a\u4f8b \u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b SQL \u8bed\u53e5\uff0c\u5b83\u5c06\u4ece &#8220;students&#8221; \u8868\u4e2d\u5220\u9664\u5177\u6709\u91cd\u590d &#8220;student_id&#8221; \u503c\u7684\u884c\uff0c\u4fdd\u7559\u552f\u4e00\u503c\uff1a DELETE FROM students WHERE student_id IN (SELECT DISTINCT student_id FROM students); \u767b\u5f55\u540e\u590d\u5236 \u6267\u884c\u8bed\u53e5 [&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-5686","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5686","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=5686"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5686\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5686"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5686"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5686"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}