{"id":5957,"date":"2024-11-14T09:42:04","date_gmt":"2024-11-14T01:42:04","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5957\/"},"modified":"2024-11-14T09:42:04","modified_gmt":"2024-11-14T01:42:04","slug":"sql%e5%a6%82%e4%bd%95%e8%bf%87%e6%bb%a4%e9%87%8d%e5%a4%8d","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5957\/","title":{"rendered":"sql\u5982\u4f55\u8fc7\u6ee4\u91cd\u590d"},"content":{"rendered":"<blockquote><p>\n  sql \u4e2d\u8fc7\u6ee4\u91cd\u590d\u8bb0\u5f55\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528 distinct \u5173\u952e\u5b57\u53bb\u9664\u91cd\u590d\u503c\u3002\u4f7f\u7528 group by \u5b50\u53e5\u5206\u7ec4\u7ed3\u679c\u5e76\u8fd4\u56de\u6bcf\u7ec4\u7684\u7b2c\u4e00\u884c\u3002\u4f7f\u7528 count() \u51fd\u6570\u67e5\u627e\u51fa\u73b0\u591a\u6b21\u7684\u503c\u5e76\u8fdb\u4e00\u6b65\u8fc7\u6ee4\u3002\u4f7f\u7528\u4e34\u65f6\u8868\u4fdd\u5b58\u552f\u4e00\u503c\u5e76\u4ece\u4e2d\u68c0\u7d22\u8bb0\u5f55\u3002\u4f7f\u7528\u7a97\u53e3\u51fd\u6570\u4e3a\u6bcf\u4e00\u884c\u5206\u914d\u552f\u4e00\u884c\u53f7\uff0c\u5e76\u9009\u62e9\u7b2c\u4e00\u4e2a\u4e0d\u91cd\u590d\u7684\u884c\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/06\/2024060609332312066.jpg\" class=\"aligncenter\" title=\"sql\u5982\u4f55\u8fc7\u6ee4\u91cd\u590d\u63d2\u56fe\" alt=\"sql\u5982\u4f55\u8fc7\u6ee4\u91cd\u590d\u63d2\u56fe\" \/><\/p>\n<p><strong>SQL \u4e2d\u8fc7\u6ee4\u91cd\u590d\u8bb0\u5f55\u7684\u65b9\u6cd5<\/strong><\/p>\n<p>\u5728 SQL \u4e2d\u8fc7\u6ee4\u91cd\u590d\u8bb0\u5f55\u6709\u51e0\u79cd\u65b9\u6cd5\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u6570\u636e\u5e93\u7cfb\u7edf\u548c\u6240\u4f7f\u7528\u7684 SQL \u65b9\u8a00\u3002<\/p>\n<p><strong>\u4f7f\u7528 DISTINCT \u5173\u952e<\/strong><\/p>\n<pre>SELECT DISTINCT column_name\nFROM table_name;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4f7f\u7528 DISTINCT \u5173\u952e\u5b57\u53ef\u4ee5\u4ece\u7ed3\u679c\u96c6\u4e2d\u53bb\u9664\u91cd\u590d\u7684\u503c\u3002\u5b83\u53ea\u8fd4\u56de\u7ed3\u679c\u96c6\u4e2d\u6bcf\u4e00\u884c\u7684\u552f\u4e00\u503c\u3002<\/p>\n<p><strong>\u4f7f\u7528 GROUP BY \u5b50\u53e5<\/strong><\/p>\n<pre>SELECT column_name\nFROM table_name\nGROUP BY column_name;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4f7f\u7528 GROUP BY \u5b50\u53e5\u53ef\u4ee5\u5c06\u7ed3\u679c\u96c6\u5206\u7ec4\uff0c\u5e76\u5c06\u6bcf\u7ec4\u7684\u7b2c\u4e00\u884c\u8fd4\u56de\u4e3a\u7ed3\u679c\u3002\u8fd9\u53ef\u4ee5\u53bb\u9664\u91cd\u590d\u503c\uff0c\u4f46\u53ef\u80fd\u4e0d\u9002\u7528\u4e8e\u6240\u6709\u60c5\u51b5\u3002<\/p>\n<p><strong>\u4f7f\u7528 COUNT() \u51fd\u6570<\/strong><\/p>\n<pre>SELECT column_name\nFROM table_name\nWHERE COUNT(*) &gt; 1;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4f7f\u7528 COUNT() \u51fd\u6570\u4e0e WHERE \u5b50\u53e5\u7ed3\u5408\u53ef\u4ee5\u67e5\u627e\u51fa\u73b0\u591a\u4e8e\u4e00\u6b21\u7684\u503c\u3002\u7136\u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u6b64\u7ed3\u679c\u96c6\u8fdb\u4e00\u6b65\u8fc7\u6ee4\u8868\u3002<\/p>\n<p><strong>\u4f7f\u7528\u4e34\u65f6\u8868<\/strong><\/p>\n<pre>CREATE TEMP TABLE unique_table AS\nSELECT DISTINCT column_name\nFROM table_name;\n\nSELECT *\nFROM unique_table;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4f7f\u7528\u4e34\u65f6\u8868\u53ef\u4ee5\u4fdd\u5b58\u7ed3\u679c\u96c6\u7684\u552f\u4e00\u503c\u3002\u7136\u540e\uff0c\u60a8\u53ef\u4ee5\u5bf9\u4e34\u65f6\u8868\u8fd0\u884c\u67e5\u8be2\u4ee5\u68c0\u7d22\u552f\u4e00\u7684\u8bb0\u5f55\u3002<\/p>\n<p><strong>\u4f7f\u7528\u7a97\u53e3\u51fd\u6570<\/strong><\/p>\n<pre>SELECT column_name, ROW_NUMBER() OVER (PARTITION BY column_name ORDER BY column_name) AS row_num\nFROM table_name;\n\nSELECT column_name\nFROM (\n    SELECT column_name, ROW_NUMBER() OVER (PARTITION BY column_name ORDER BY column_name) AS row_num\n    FROM table_name\n) AS subquery\nWHERE row_num = 1;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4f7f\u7528\u7a97\u53e3\u51fd\u6570\u53ef\u4ee5\u4e3a\u6bcf\u4e00\u884c\u7684\u7279\u5b9a\u5217\u5206\u914d\u4e00\u4e2a\u552f\u4e00\u7684\u884c\u53f7\u3002\u7136\u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u6b64\u884c\u53f7\u6765\u4ec5\u9009\u62e9\u7b2c\u4e00\u884c\uff08\u4e0d\u91cd\u590d\uff09\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fsql\u5982\u4f55\u8fc7\u6ee4\u91cd\u590d\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\u8fc7\u6ee4\u91cd\u590d\u8bb0\u5f55\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528 distinct \u5173\u952e\u5b57\u53bb\u9664\u91cd\u590d\u503c\u3002\u4f7f\u7528 group by \u5b50\u53e5\u5206\u7ec4\u7ed3\u679c\u5e76\u8fd4\u56de\u6bcf\u7ec4\u7684\u7b2c\u4e00\u884c\u3002\u4f7f\u7528 count() \u51fd\u6570\u67e5\u627e\u51fa\u73b0\u591a\u6b21\u7684\u503c\u5e76\u8fdb\u4e00\u6b65\u8fc7\u6ee4\u3002\u4f7f\u7528\u4e34\u65f6\u8868\u4fdd\u5b58\u552f\u4e00\u503c\u5e76\u4ece\u4e2d\u68c0\u7d22\u8bb0\u5f55\u3002\u4f7f\u7528\u7a97\u53e3\u51fd\u6570\u4e3a\u6bcf\u4e00\u884c\u5206\u914d\u552f\u4e00\u884c\u53f7\uff0c\u5e76\u9009\u62e9\u7b2c\u4e00\u4e2a\u4e0d\u91cd\u590d\u7684\u884c\u3002 SQL \u4e2d\u8fc7\u6ee4\u91cd\u590d\u8bb0\u5f55\u7684\u65b9\u6cd5 \u5728 SQL \u4e2d\u8fc7\u6ee4\u91cd\u590d\u8bb0\u5f55\u6709\u51e0\u79cd\u65b9\u6cd5\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u6570\u636e\u5e93\u7cfb\u7edf\u548c\u6240\u4f7f\u7528\u7684 SQL \u65b9\u8a00\u3002 \u4f7f\u7528 DISTINCT \u5173\u952e SELECT DISTINCT column_name FROM table_name; \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528 DISTINCT \u5173\u952e\u5b57\u53ef\u4ee5\u4ece\u7ed3\u679c\u96c6\u4e2d\u53bb\u9664\u91cd\u590d\u7684\u503c\u3002\u5b83\u53ea\u8fd4\u56de\u7ed3\u679c\u96c6\u4e2d\u6bcf\u4e00\u884c\u7684\u552f\u4e00\u503c\u3002 \u4f7f\u7528 GROUP BY \u5b50\u53e5 SELECT column_name FROM table_name GROUP BY column_name; \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528 GROUP BY \u5b50\u53e5\u53ef\u4ee5\u5c06\u7ed3\u679c\u96c6\u5206\u7ec4\uff0c\u5e76\u5c06\u6bcf\u7ec4\u7684\u7b2c\u4e00\u884c\u8fd4\u56de\u4e3a\u7ed3\u679c\u3002\u8fd9\u53ef\u4ee5\u53bb\u9664\u91cd\u590d\u503c\uff0c\u4f46\u53ef\u80fd\u4e0d\u9002\u7528\u4e8e\u6240\u6709\u60c5\u51b5\u3002 \u4f7f\u7528 COUNT() \u51fd\u6570 SELECT column_name FROM table_name WHERE COUNT(*) &gt; 1; [&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-5957","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5957","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=5957"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5957\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5957"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5957"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5957"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}