{"id":157854,"date":"2024-11-03T00:00:21","date_gmt":"2024-11-02T16:00:21","guid":{"rendered":"https:\/\/server.hk\/cnblog\/157854\/"},"modified":"2024-11-03T00:00:22","modified_gmt":"2024-11-02T16:00:22","slug":"%e9%ab%98%e6%95%88%e7%9a%84%e6%95%b8%e6%93%9a%e5%ba%ab%e6%9f%a5%e8%a9%a2%e5%8e%bb%e9%87%8d%e6%8a%80%e5%b7%a7%ef%bc%88%e6%95%b8%e6%93%9a%e5%ba%ab%e6%9f%a5%e8%a9%a2-%e5%8e%bb%e9%87%8d%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/157854\/","title":{"rendered":"\u9ad8\u6548\u7684\u6578\u64da\u5eab\u67e5\u8a62\u53bb\u91cd\u6280\u5de7\uff08\u6578\u64da\u5eab\u67e5\u8a62 \u53bb\u91cd\uff09"},"content":{"rendered":"<h1 id=\"%e9%ab%98%e6%95%88%e7%9a%84%e6%95%b8%e6%93%9a%e5%ba%ab%e6%9f%a5%e8%a9%a2%e5%8e%bb%e9%87%8d%e6%8a%80%e5%b7%a7%ef%bc%88%e6%95%b8%e6%93%9a%e5%ba%ab%e6%9f%a5%e8%a9%a2-%e5%8e%bb%e9%87%8d%ef%bc%89-SjitWCYaWP\">\u9ad8\u6548\u7684\u6578\u64da\u5eab\u67e5\u8a62\u53bb\u91cd\u6280\u5de7\uff08\u6578\u64da\u5eab\u67e5\u8a62 \u53bb\u91cd\uff09<\/h1>\n<p>\u5728\u6578\u64da\u5eab\u7ba1\u7406\u4e2d\uff0c\u6578\u64da\u53bb\u91cd\u662f\u4e00\u500b\u91cd\u8981\u7684\u8ab2\u984c\u3002\u96a8\u8457\u6578\u64da\u91cf\u7684\u589e\u9577\uff0c\u91cd\u8907\u6578\u64da\u4e0d\u50c5\u6703\u4f54\u7528\u5b58\u5132\u7a7a\u9593\uff0c\u9084\u6703\u5f71\u97ff\u67e5\u8a62\u6027\u80fd\u548c\u6578\u64da\u7684\u6e96\u78ba\u6027\u3002\u56e0\u6b64\uff0c\u638c\u63e1\u9ad8\u6548\u7684\u6578\u64da\u5eab\u67e5\u8a62\u53bb\u91cd\u6280\u5de7\u5c0d\u65bc\u6578\u64da\u5eab\u7ba1\u7406\u8005\u4f86\u8aaa\u81f3\u95dc\u91cd\u8981\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%9c%80%e8%a6%81%e5%8e%bb%e9%87%8d%ef%bc%9f-SjitWCYaWP\">\u70ba\u4ec0\u9ebc\u9700\u8981\u53bb\u91cd\uff1f<\/h2>\n<p>\u6578\u64da\u53bb\u91cd\u7684\u4e3b\u8981\u76ee\u7684\u662f\u63d0\u9ad8\u6578\u64da\u7684\u8cea\u91cf\u548c\u67e5\u8a62\u6548\u7387\u3002\u91cd\u8907\u7684\u6578\u64da\u6703\u5c0e\u81f4\u4ee5\u4e0b\u554f\u984c\uff1a<\/p>\n<ul>\n<li>\u4f54\u7528\u984d\u5916\u7684\u5b58\u5132\u7a7a\u9593\u3002<\/li>\n<li>\u5f71\u97ff\u67e5\u8a62\u7d50\u679c\u7684\u6e96\u78ba\u6027\u3002<\/li>\n<li>\u589e\u52a0\u6578\u64da\u8655\u7406\u7684\u6642\u9593\u548c\u6210\u672c\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%b8%b8%e8%a6%8b%e7%9a%84%e5%8e%bb%e9%87%8d%e6%96%b9%e6%b3%95-SjitWCYaWP\">\u5e38\u898b\u7684\u53bb\u91cd\u65b9\u6cd5<\/h2>\n<p>\u5728\u6578\u64da\u5eab\u4e2d\uff0c\u6709\u591a\u7a2e\u65b9\u6cd5\u53ef\u4ee5\u5be6\u73fe\u6578\u64da\u53bb\u91cd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u898b\u7684\u6280\u5de7\uff1a<\/p>\n<h3 id=\"1-%e4%bd%bf%e7%94%a8-distinct-%e9%97%9c%e9%8d%b5%e5%ad%97-SjitWCYaWP\">1. \u4f7f\u7528 DISTINCT \u95dc\u9375\u5b57<\/h3>\n<p>\u5728 SQL \u67e5\u8a62\u4e2d\uff0c\u4f7f\u7528 <code>DISTINCT<\/code> \u95dc\u9375\u5b57\u53ef\u4ee5\u8f15\u9b06\u53bb\u9664\u91cd\u8907\u7684\u8a18\u9304\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>SELECT DISTINCT column_name FROM table_name;<\/code><\/pre>\n<p>\u9019\u689d\u67e5\u8a62\u5c07\u8fd4\u56de\u6307\u5b9a\u5217\u4e2d\u6240\u6709\u552f\u4e00\u7684\u503c\uff0c\u5f9e\u800c\u53bb\u9664\u91cd\u8907\u9805\u3002<\/p>\n<h3 id=\"2-group-by-%e5%ad%90%e5%8f%a5-SjitWCYaWP\">2. GROUP BY \u5b50\u53e5<\/h3>\n<p>\u53e6\u4e00\u7a2e\u5e38\u7528\u7684\u53bb\u91cd\u65b9\u6cd5\u662f\u4f7f\u7528 <code>GROUP BY<\/code> \u5b50\u53e5\u3002\u9019\u4e0d\u50c5\u53ef\u4ee5\u53bb\u9664\u91cd\u8907\u7684\u6578\u64da\uff0c\u9084\u53ef\u4ee5\u9032\u884c\u805a\u5408\u8a08\u7b97\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>SELECT column_name, COUNT(*) FROM table_name GROUP BY column_name;<\/code><\/pre>\n<p>\u9019\u689d\u67e5\u8a62\u5c07\u8fd4\u56de\u6bcf\u500b\u552f\u4e00\u503c\u53ca\u5176\u51fa\u73fe\u7684\u6b21\u6578\u3002<\/p>\n<h3 id=\"3-%e4%bd%bf%e7%94%a8%e7%aa%97%e5%8f%a3%e5%87%bd%e6%95%b8-SjitWCYaWP\">3. \u4f7f\u7528\u7a97\u53e3\u51fd\u6578<\/h3>\n<p>\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0c\u4f7f\u7528\u7a97\u53e3\u51fd\u6578\u53ef\u4ee5\u66f4\u9748\u6d3b\u5730\u8655\u7406\u53bb\u91cd\u554f\u984c\u3002\u4f8b\u5982\uff0c\u4f7f\u7528 <code>ROW_NUMBER()<\/code> \u51fd\u6578\u53ef\u4ee5\u70ba\u6bcf\u4e00\u884c\u5206\u914d\u4e00\u500b\u552f\u4e00\u7684\u5e8f\u865f\uff0c\u7136\u5f8c\u6839\u64da\u9019\u500b\u5e8f\u865f\u9032\u884c\u904e\u6ffe\uff1a<\/p>\n<pre><code>WITH RankedData AS (\n    SELECT column_name, ROW_NUMBER() OVER (PARTITION BY column_name ORDER BY id) AS rn\n    FROM table_name\n)\nSELECT column_name FROM RankedData WHERE rn = 1;<\/code><\/pre>\n<p>\u9019\u6a23\u53ef\u4ee5\u4fdd\u7559\u6bcf\u7d44\u91cd\u8907\u6578\u64da\u4e2d\u7684\u7b2c\u4e00\u689d\u8a18\u9304\u3002<\/p>\n<h3 id=\"4-%e4%bd%bf%e7%94%a8-join-%e9%80%b2%e8%a1%8c%e5%8e%bb%e9%87%8d-SjitWCYaWP\">4. \u4f7f\u7528 JOIN \u9032\u884c\u53bb\u91cd<\/h3>\n<p>\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>JOIN<\/code> \u4f86\u53bb\u9664\u91cd\u8907\u6578\u64da\u3002\u4f8b\u5982\uff0c\u901a\u904e\u5c07\u8868\u8207\u81ea\u8eab\u9032\u884c\u9023\u63a5\uff0c\u53ef\u4ee5\u9078\u64c7\u6027\u5730\u4fdd\u7559\u552f\u4e00\u8a18\u9304\uff1a<\/p>\n<pre><code>SELECT a.column_name\nFROM table_name a\nLEFT JOIN table_name b ON a.column_name = b.column_name AND a.id &lt; b.id\nWHERE b.id IS NULL;<\/code><\/pre>\n<p>\u9019\u689d\u67e5\u8a62\u5c07\u8fd4\u56de\u6240\u6709\u552f\u4e00\u7684\u8a18\u9304\uff0c\u4e26\u53bb\u9664\u91cd\u8907\u9805\u3002<\/p>\n<h2 id=\"%e6%80%a7%e8%83%bd%e8%80%83%e9%87%8f-SjitWCYaWP\">\u6027\u80fd\u8003\u91cf<\/h2>\n<p>\u5728\u9032\u884c\u6578\u64da\u53bb\u91cd\u6642\uff0c\u6027\u80fd\u662f\u4e00\u500b\u91cd\u8981\u7684\u8003\u91cf\u56e0\u7d20\u3002\u4f7f\u7528 <code>DISTINCT<\/code> \u548c <code>GROUP BY<\/code> \u53ef\u80fd\u6703\u5c0e\u81f4\u5168\u8868\u6383\u63cf\uff0c\u7279\u5225\u662f\u5728\u6578\u64da\u91cf\u5927\u7684\u60c5\u6cc1\u4e0b\u3002\u56e0\u6b64\uff0c\u5efa\u8b70\u5728\u8a2d\u8a08\u6578\u64da\u5eab\u6642\uff0c\u8003\u616e\u4f7f\u7528\u7d22\u5f15\u4f86\u63d0\u9ad8\u67e5\u8a62\u6027\u80fd\u3002\u6b64\u5916\uff0c\u5b9a\u671f\u6e05\u7406\u6578\u64da\u5eab\u4e2d\u7684\u91cd\u8907\u6578\u64da\u4e5f\u662f\u4e00\u500b\u826f\u597d\u7684\u7fd2\u6163\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-SjitWCYaWP\">\u7e3d\u7d50<\/h2>\n<p>\u6578\u64da\u5eab\u67e5\u8a62\u53bb\u91cd\u662f\u6578\u64da\u7ba1\u7406\u4e2d\u7684\u4e00\u500b\u91cd\u8981\u74b0\u7bc0\uff0c\u638c\u63e1\u9ad8\u6548\u7684\u53bb\u91cd\u6280\u5de7\u80fd\u5920\u986f\u8457\u63d0\u9ad8\u6578\u64da\u7684\u8cea\u91cf\u548c\u67e5\u8a62\u6027\u80fd\u3002\u7121\u8ad6\u662f\u4f7f\u7528 <code>DISTINCT<\/code>\u3001<code>GROUP BY<\/code> \u9084\u662f\u7a97\u53e3\u51fd\u6578\uff0c\u9078\u64c7\u5408\u9069\u7684\u65b9\u6cd5\u90fd\u80fd\u5e6b\u52a9\u6211\u5011\u66f4\u597d\u5730\u7ba1\u7406\u6578\u64da\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6548\u6578\u64da\u8655\u7406\u7684\u4f01\u696d\uff0c\u9078\u64c7\u5408\u9069\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4e5f\u80fd\u63d0\u4f9b\u66f4\u597d\u7684\u6027\u80fd\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u638c\u63e1\u9ad8\u6548\u7684\u6578\u64da\u5eab\u67e5\u8a62\u53bb\u91cd\u6280\u5de7\uff0c\u63d0\u5347\u6578\u64da\u8655\u7406\u6548\u7387\uff0c\u78ba\u4fdd\u7d50\u679c\u6e96\u78ba\u7121\u91cd\u8907\uff0c\u512a\u5316\u6578\u64da\u7ba1\u7406\u6d41\u7a0b\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-157854","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/157854","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=157854"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/157854\/revisions"}],"predecessor-version":[{"id":157855,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/157854\/revisions\/157855"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=157854"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=157854"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=157854"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}