{"id":186252,"date":"2024-11-11T04:22:22","date_gmt":"2024-11-10T20:22:22","guid":{"rendered":"https:\/\/server.hk\/cnblog\/186252\/"},"modified":"2024-11-11T04:22:23","modified_gmt":"2024-11-10T20:22:23","slug":"%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%e6%b7%ba%e6%9e%90%ef%bc%88%e6%95%b8%e6%93%9a%e5%ba%ab%e6%9f%a5%e8%a9%a2%e5%a6%82%e4%bd%95%e6%b6%88%e9%99%a4%e9%87%8d","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/186252\/","title":{"rendered":"\u6578\u64da\u5eab\u67e5\u8a62\u53bb\u91cd\u6280\u5de7\u6dfa\u6790\uff08\u6578\u64da\u5eab\u67e5\u8a62\u5982\u4f55\u6d88\u9664\u91cd\u8907\uff09"},"content":{"rendered":"<h1 id=\"%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%e6%b7%ba%e6%9e%90%ef%bc%88%e6%95%b8%e6%93%9a%e5%ba%ab%e6%9f%a5%e8%a9%a2%e5%a6%82%e4%bd%95%e6%b6%88%e9%99%a4%e9%87%8d-soRXPfkGlf\">\u6578\u64da\u5eab\u67e5\u8a62\u53bb\u91cd\u6280\u5de7\u6dfa\u6790\uff08\u6578\u64da\u5eab\u67e5\u8a62\u5982\u4f55\u6d88\u9664\u91cd\u8907\uff09<\/h1>\n<p>\u5728\u6578\u64da\u5eab\u7ba1\u7406\u4e2d\uff0c\u91cd\u8907\u6578\u64da\u7684\u5b58\u5728\u4e0d\u50c5\u6703\u5f71\u97ff\u67e5\u8a62\u6548\u7387\uff0c\u9084\u53ef\u80fd\u5c0e\u81f4\u6578\u64da\u4e0d\u4e00\u81f4\u6027\u3002\u56e0\u6b64\uff0c\u5b78\u6703\u5982\u4f55\u6709\u6548\u5730\u6d88\u9664\u91cd\u8907\u6578\u64da\u662f\u6bcf\u4f4d\u6578\u64da\u5eab\u7ba1\u7406\u54e1\u548c\u958b\u767c\u8005\u5fc5\u9808\u638c\u63e1\u7684\u6280\u80fd\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u4e00\u4e9b\u5e38\u898b\u7684\u6578\u64da\u5eab\u67e5\u8a62\u53bb\u91cd\u6280\u5de7\uff0c\u5e6b\u52a9\u8b80\u8005\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u5728\u67e5\u8a62\u4e2d\u6d88\u9664\u91cd\u8907\u6578\u64da\u3002<\/p>\n<h2 id=\"1-%e4%bd%bf%e7%94%a8-distinct-%e9%97%9c%e9%8d%b5%e5%ad%97-soRXPfkGlf\">1. \u4f7f\u7528 DISTINCT \u95dc\u9375\u5b57<\/h2>\n<p>\u5728 SQL \u67e5\u8a62\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u53bb\u91cd\u65b9\u6cd5\u662f\u4f7f\u7528 <code>DISTINCT<\/code> \u95dc\u9375\u5b57\u3002\u9019\u500b\u95dc\u9375\u5b57\u53ef\u4ee5\u7528\u4f86\u9078\u64c7\u552f\u4e00\u7684\u6578\u64da\u884c\uff0c\u5f9e\u800c\u6d88\u9664\u91cd\u8907\u9805\u3002<\/p>\n<pre><code>SELECT DISTINCT column1, column2 FROM table_name;<\/code><\/pre>\n<p>\u4f8b\u5982\uff0c\u5047\u8a2d\u6709\u4e00\u500b\u540d\u70ba <code>customers<\/code> \u7684\u8868\uff0c\u5176\u4e2d\u5305\u542b\u5ba2\u6236\u7684\u59d3\u540d\u548c\u57ce\u5e02\u3002\u5982\u679c\u6211\u5011\u60f3\u8981\u7372\u53d6\u6240\u6709\u4e0d\u540c\u7684\u57ce\u5e02\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8a62\uff1a<\/p>\n<pre><code>SELECT DISTINCT city FROM customers;<\/code><\/pre>\n<h2 id=\"2-%e4%bd%bf%e7%94%a8-group-by-%e5%ad%90%e5%8f%a5-soRXPfkGlf\">2. \u4f7f\u7528 GROUP BY \u5b50\u53e5<\/h2>\n<p>\u53e6\u4e00\u7a2e\u5e38\u898b\u7684\u53bb\u91cd\u65b9\u6cd5\u662f\u4f7f\u7528 <code>GROUP BY<\/code> \u5b50\u53e5\u3002\u9019\u500b\u5b50\u53e5\u4e0d\u50c5\u53ef\u4ee5\u7528\u4f86\u53bb\u91cd\uff0c\u9084\u53ef\u4ee5\u9032\u884c\u805a\u5408\u8a08\u7b97\u3002<\/p>\n<pre><code>SELECT column1, COUNT(*) FROM table_name GROUP BY column1;<\/code><\/pre>\n<p>\u4f8b\u5982\uff0c\u5982\u679c\u6211\u5011\u60f3\u8981\u8a08\u7b97\u6bcf\u500b\u57ce\u5e02\u7684\u5ba2\u6236\u6578\u91cf\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8a62\uff1a<\/p>\n<pre><code>SELECT city, COUNT(*) FROM customers GROUP BY city;<\/code><\/pre>\n<h2 id=\"3-%e4%bd%bf%e7%94%a8%e5%ad%90%e6%9f%a5%e8%a9%a2-soRXPfkGlf\">3. \u4f7f\u7528\u5b50\u67e5\u8a62<\/h2>\n<p>\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0c\u4f7f\u7528\u5b50\u67e5\u8a62\u53ef\u4ee5\u66f4\u9748\u6d3b\u5730\u53bb\u9664\u91cd\u8907\u6578\u64da\u3002\u5b50\u67e5\u8a62\u53ef\u4ee5\u5728\u4e3b\u67e5\u8a62\u4e2d\u8fd4\u56de\u552f\u4e00\u7684\u503c\uff0c\u7136\u5f8c\u518d\u9032\u884c\u9032\u4e00\u6b65\u7684\u67e5\u8a62\u3002<\/p>\n<pre><code>SELECT * FROM customers WHERE city IN (SELECT DISTINCT city FROM customers);<\/code><\/pre>\n<p>\u9019\u6a23\u7684\u67e5\u8a62\u53ef\u4ee5\u78ba\u4fdd\u6211\u5011\u53ea\u7372\u53d6\u5b58\u5728\u65bc <code>customers<\/code> \u8868\u4e2d\u7684\u552f\u4e00\u57ce\u5e02\u3002<\/p>\n<h2 id=\"4-%e4%bd%bf%e7%94%a8%e7%aa%97%e5%8f%a3%e5%87%bd%e6%95%b8-soRXPfkGlf\">4. \u4f7f\u7528\u7a97\u53e3\u51fd\u6578<\/h2>\n<p>\u5728\u4e00\u4e9b\u9ad8\u7d1a\u7684\u6578\u64da\u5eab\u7cfb\u7d71\u4e2d\uff0c\u7a97\u53e3\u51fd\u6578\u63d0\u4f9b\u4e86\u4e00\u7a2e\u66f4\u9748\u6d3b\u7684\u53bb\u91cd\u65b9\u5f0f\u3002\u4f7f\u7528\u7a97\u53e3\u51fd\u6578\u53ef\u4ee5\u5728\u67e5\u8a62\u4e2d\u4fdd\u7559\u91cd\u8907\u6578\u64da\u7684\u67d0\u4e9b\u884c\uff0c\u4e26\u6839\u64da\u7279\u5b9a\u689d\u4ef6\u9032\u884c\u904e\u6ffe\u3002<\/p>\n<pre><code>SELECT column1, ROW_NUMBER() OVER (PARTITION BY column1 ORDER BY column2) as row_num FROM table_name;<\/code><\/pre>\n<p>\u4f8b\u5982\uff0c\u5982\u679c\u6211\u5011\u60f3\u8981\u7372\u53d6\u6bcf\u500b\u57ce\u5e02\u7684\u7b2c\u4e00\u500b\u5ba2\u6236\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8a62\uff1a<\/p>\n<pre><code>SELECT * FROM (SELECT city, customer_name, ROW_NUMBER() OVER (PARTITION BY city ORDER BY customer_name) as row_num FROM customers) as temp WHERE row_num = 1;<\/code><\/pre>\n<h2 id=\"5-%e6%95%b8%e6%93%9a%e6%b8%85%e7%90%86%e8%88%87%e9%a0%90%e8%99%95%e7%90%86-soRXPfkGlf\">5. \u6578\u64da\u6e05\u7406\u8207\u9810\u8655\u7406<\/h2>\n<p>\u9664\u4e86\u5728\u67e5\u8a62\u4e2d\u53bb\u91cd\uff0c\u6578\u64da\u6e05\u7406\u548c\u9810\u8655\u7406\u4e5f\u662f\u6d88\u9664\u91cd\u8907\u6578\u64da\u7684\u91cd\u8981\u6b65\u9a5f\u3002\u5b9a\u671f\u6aa2\u67e5\u548c\u6e05\u7406\u6578\u64da\u5eab\u4e2d\u7684\u91cd\u8907\u6578\u64da\uff0c\u53ef\u4ee5\u63d0\u9ad8\u6578\u64da\u7684\u8cea\u91cf\u548c\u67e5\u8a62\u7684\u6548\u7387\u3002<\/p>\n<ul>\n<li>\u5b9a\u671f\u57f7\u884c\u6578\u64da\u6e05\u7406\u4efb\u52d9\u3002<\/li>\n<li>\u4f7f\u7528\u6578\u64da\u9a57\u8b49\u898f\u5247\u4f86\u9632\u6b62\u91cd\u8907\u6578\u64da\u7684\u63d2\u5165\u3002<\/li>\n<li>\u8003\u616e\u4f7f\u7528\u552f\u4e00\u7d04\u675f\u4f86\u5f37\u5236\u6578\u64da\u7684\u552f\u4e00\u6027\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-soRXPfkGlf\">\u7e3d\u7d50<\/h2>\n<p>\u5728\u6578\u64da\u5eab\u67e5\u8a62\u4e2d\uff0c\u6d88\u9664\u91cd\u8907\u6578\u64da\u662f\u63d0\u9ad8\u6578\u64da\u8cea\u91cf\u548c\u67e5\u8a62\u6548\u7387\u7684\u91cd\u8981\u6b65\u9a5f\u3002\u901a\u904e\u4f7f\u7528 <code>DISTINCT<\/code>\u3001<code>GROUP BY<\/code>\u3001\u5b50\u67e5\u8a62\u548c\u7a97\u53e3\u51fd\u6578\u7b49\u6280\u8853\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u6709\u6548\u5730\u53bb\u9664\u91cd\u8907\u6578\u64da\u3002\u6b64\u5916\uff0c\u5b9a\u671f\u7684\u6578\u64da\u6e05\u7406\u548c\u9810\u8655\u7406\u4e5f\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\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> \u6216 <a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a> \u89e3\u6c7a\u65b9\u6848\uff0c\u53ef\u4ee5\u9032\u4e00\u6b65\u63d0\u5347\u6578\u64da\u5eab\u7684\u6027\u80fd\u548c\u7a69\u5b9a\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u6578\u64da\u5eab\u67e5\u8a62\u53bb\u91cd\u6280\u5de7\uff0c\u5b78\u7fd2\u5982\u4f55\u6709\u6548\u6d88\u9664\u91cd\u8907\u6578\u64da\uff0c\u63d0\u5347\u67e5\u8a62\u6548\u7387\u8207\u6578\u64da\u6e96\u78ba\u6027\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-186252","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/186252","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=186252"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/186252\/revisions"}],"predecessor-version":[{"id":186253,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/186252\/revisions\/186253"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=186252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=186252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=186252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}