{"id":5897,"date":"2024-11-14T08:09:33","date_gmt":"2024-11-14T00:09:33","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5897\/"},"modified":"2024-11-14T08:09:33","modified_gmt":"2024-11-14T00:09:33","slug":"sql-distinct%e6%80%8e%e4%b9%88%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5897\/","title":{"rendered":"sql distinct\u600e\u4e48\u7528"},"content":{"rendered":"<blockquote><p>\n  distinct \u64cd\u4f5c\u7b26\u7528\u4e8e sql \u67e5\u8be2\u4e2d\u6392\u9664\u91cd\u590d\u884c\uff0c\u4ec5\u8fd4\u56de\u552f\u4e00\u503c\uff0c\u9002\u7528\u4e8e\u83b7\u53d6\u552f\u4e00\u503c\u5217\u8868\u3001\u7edf\u8ba1\u552f\u4e00\u503c\u6570\u91cf\u548c\u7ed3\u5408 group by \u4f7f\u7528\u7b49\u573a\u666f\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/03\/2024060321543734294.jpg\" class=\"aligncenter\" title=\"sql distinct\u600e\u4e48\u7528\u63d2\u56fe\" alt=\"sql distinct\u600e\u4e48\u7528\u63d2\u56fe\" \/><\/p>\n<p><strong>DISTINCT \u5728 SQL \u4e2d\u7684\u7528\u6cd5<\/strong><\/p>\n<p>DISTINCT \u64cd\u4f5c\u7b26\u7528\u4e8e\u4ece SQL \u67e5\u8be2\u7ed3\u679c\u4e2d\u6392\u9664\u91cd\u590d\u7684\u884c\uff0c\u4ec5\u8fd4\u56de\u552f\u4e00\u7684\u503c\u3002\u5b83\u7528\u4e8e\u786e\u4fdd\u7ed3\u679c\u96c6\u4e2d\u4e0d\u5305\u542b\u4efb\u4f55\u91cd\u590d\u6570\u636e\u3002<\/p>\n<p><strong>\u8bed\u6cd5<\/strong><\/p>\n<pre>SELECT DISTINCT column_name(s)\nFROM table_name;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li> <strong>column_name(s)<\/strong>\uff1a\u6307\u5b9a\u8981\u5bf9\u5176\u8fdb\u884c DISTINCT \u64cd\u4f5c\u7684\u5217\u3002\u53ef\u4ee5\u6307\u5b9a\u591a\u4e2a\u5217\u3002<\/li>\n<li> <strong>table_name<\/strong>\uff1a\u8981\u67e5\u8be2\u7684\u6570\u636e\u8868\u3002<\/li>\n<\/ul>\n<p><strong>\u7528\u6cd5<\/strong><\/p>\n<p>\u4f7f\u7528 DISTINCT \u64cd\u4f5c\u7b26\u7684\u5e38\u89c1\u573a\u666f\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\n<p><strong>\u83b7\u53d6\u8868\u4e2d\u552f\u4e00\u503c\u5217\u8868\uff1a<\/strong><\/p>\n<pre>SELECT DISTINCT name\nFROM employees;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  <\/li>\n<li>\n<p><strong>\u7edf\u8ba1\u552f\u4e00\u503c\u7684\u6570\u91cf\uff1a<\/strong><\/p>\n<pre>SELECT COUNT(DISTINCT name)\nFROM employees;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  <\/li>\n<li>\n<p><strong>\u7ed3\u5408 GROUP BY \u4f7f\u7528\uff1a<\/strong><br \/>DISTINCT \u53ef\u4ee5\u4e0e GROUP BY \u4e00\u8d77\u4f7f\u7528\uff0c\u5bf9\u7279\u5b9a\u6761\u4ef6\u4e0b\u7684\u552f\u4e00\u503c\u8fdb\u884c\u5206\u7ec4\u548c\u8ba1\u6570\u3002<\/p>\n<pre>SELECT department, COUNT(DISTINCT employee_id)\nFROM employees\nGROUP BY department;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  <\/li>\n<\/ul>\n<p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<ul>\n<li>DISTINCT \u64cd\u4f5c\u7b26\u53ea\u80fd\u7528\u4e8e SELECT \u8bed\u53e5\u3002<\/li>\n<li>\u5982\u679c\u5728\u5217\u4e2d\u4f7f\u7528 DISTINCT\uff0c\u5219\u5176\u4ed6\u5217\u4e2d\u7684\u503c\u4e5f\u4f1a\u88ab\u6392\u9664\uff0c\u5373\u4f7f\u5b83\u4eec\u4e0d\u91cd\u590d\u3002<\/li>\n<li>DISTINCT \u64cd\u4f5c\u7b26\u5728\u5927\u578b\u6570\u636e\u96c6\u4e0a\u53ef\u80fd\u4f1a\u5f71\u54cd\u67e5\u8be2\u6027\u80fd\uff0c\u56e0\u4e3a\u5b83\u9700\u8981\u989d\u5916\u7684\u6392\u5e8f\u548c\u6bd4\u8f83\u6b65\u9aa4\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fsql distinct\u600e\u4e48\u7528\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>distinct \u64cd\u4f5c\u7b26\u7528\u4e8e sql \u67e5\u8be2\u4e2d\u6392\u9664\u91cd\u590d\u884c\uff0c\u4ec5\u8fd4\u56de\u552f\u4e00\u503c\uff0c\u9002\u7528\u4e8e\u83b7\u53d6\u552f\u4e00\u503c\u5217\u8868\u3001\u7edf\u8ba1\u552f\u4e00\u503c\u6570\u91cf\u548c\u7ed3\u5408 group by \u4f7f\u7528\u7b49\u573a\u666f\u3002 DISTINCT \u5728 SQL \u4e2d\u7684\u7528\u6cd5 DISTINCT \u64cd\u4f5c\u7b26\u7528\u4e8e\u4ece SQL \u67e5\u8be2\u7ed3\u679c\u4e2d\u6392\u9664\u91cd\u590d\u7684\u884c\uff0c\u4ec5\u8fd4\u56de\u552f\u4e00\u7684\u503c\u3002\u5b83\u7528\u4e8e\u786e\u4fdd\u7ed3\u679c\u96c6\u4e2d\u4e0d\u5305\u542b\u4efb\u4f55\u91cd\u590d\u6570\u636e\u3002 \u8bed\u6cd5 SELECT DISTINCT column_name(s) FROM table_name; \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff1a column_name(s)\uff1a\u6307\u5b9a\u8981\u5bf9\u5176\u8fdb\u884c DISTINCT \u64cd\u4f5c\u7684\u5217\u3002\u53ef\u4ee5\u6307\u5b9a\u591a\u4e2a\u5217\u3002 table_name\uff1a\u8981\u67e5\u8be2\u7684\u6570\u636e\u8868\u3002 \u7528\u6cd5 \u4f7f\u7528 DISTINCT \u64cd\u4f5c\u7b26\u7684\u5e38\u89c1\u573a\u666f\u5305\u62ec\uff1a \u83b7\u53d6\u8868\u4e2d\u552f\u4e00\u503c\u5217\u8868\uff1a SELECT DISTINCT name FROM employees; \u767b\u5f55\u540e\u590d\u5236 \u7edf\u8ba1\u552f\u4e00\u503c\u7684\u6570\u91cf\uff1a SELECT COUNT(DISTINCT name) FROM employees; \u767b\u5f55\u540e\u590d\u5236 \u7ed3\u5408 GROUP BY \u4f7f\u7528\uff1aDISTINCT \u53ef\u4ee5\u4e0e GROUP BY \u4e00\u8d77\u4f7f\u7528\uff0c\u5bf9\u7279\u5b9a\u6761\u4ef6\u4e0b\u7684\u552f\u4e00\u503c\u8fdb\u884c\u5206\u7ec4\u548c\u8ba1\u6570\u3002 SELECT department, [&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-5897","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5897","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=5897"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5897\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5897"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5897"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5897"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}