{"id":5964,"date":"2024-11-14T11:50:35","date_gmt":"2024-11-14T03:50:35","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5964\/"},"modified":"2024-11-14T11:50:35","modified_gmt":"2024-11-14T03:50:35","slug":"sql-distinct%e6%80%8e%e4%b9%88%e7%94%a8-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5964\/","title":{"rendered":"sql\u00a0distinct\u600e\u4e48\u7528"},"content":{"rendered":"<blockquote><p>\n  sql distinct \u5173\u952e\u5b57\u901a\u8fc7\u6bd4\u8f83\u6240\u6709\u5217\u503c\u6765\u53bb\u9664\u91cd\u590d\u884c\uff0c\u53ea\u4fdd\u7559\u552f\u4e00\u7684\u884c\u3002\u7528\u6cd5\u5305\u62ec\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u6307\u5b9a\u8981\u9009\u62e9\u7684\u5217\u540d\uff0c\u540e\u8ddf distinct \u5173\u952e\u5b57\u3002\u6307\u5b9a\u8981\u67e5\u8be2\u7684\u8868\u540d\u3002distinct \u53ef\u4ee5\u5e94\u7528\u4e8e\u4e00\u4e2a\u6216\u591a\u4e2a\u5217\u3002\u4e0e group by \u4e0d\u540c\uff0cdistinct \u76f4\u63a5\u53bb\u9664\u91cd\u590d\u884c\u800c\u4e0d\u8fdb\u884c\u5206\u7ec4\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/12\/2024061202482156100.jpg\" class=\"aligncenter\" title=\"sql\u00a0distinct\u600e\u4e48\u7528\u63d2\u56fe\" alt=\"sql\u00a0distinct\u600e\u4e48\u7528\u63d2\u56fe\" \/><\/p>\n<p><strong>SQL DISTINCT\uff1a\u53bb\u9664\u91cd\u590d\u503c<\/strong><\/p>\n<p>SQL DISTINCT \u5173\u952e\u5b57\u7528\u4e8e\u4ece\u67e5\u8be2\u7ed3\u679c\u4e2d\u53bb\u9664\u91cd\u590d\u884c\uff0c\u53ea\u4fdd\u7559\u552f\u4e00\u7684\u884c\u3002\u5b83\u901a\u8fc7\u6bd4\u8f83\u6bcf\u4e00\u884c\u7684\u6240\u6709\u5217\u503c\u6765\u786e\u5b9a\u552f\u4e00\u6027\u3002<\/p>\n<p><strong>\u8bed\u6cd5<\/strong><\/p>\n<pre>SELECT DISTINCT column_1, column_2, ...\nFROM table_name;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li>column_1, column_2, &#8230;\uff1a\u8981\u9009\u62e9\u7684\u5217\u540d<\/li>\n<li>table_name\uff1a\u8981\u67e5\u8be2\u7684\u8868\u540d<\/li>\n<\/ul>\n<p><strong>\u7528\u6cd5<\/strong><\/p>\n<p>DISTINCT \u5173\u952e\u5b57\u53ef\u4ee5\u5e94\u7528\u4e8e\u4e00\u4e2a\u6216\u591a\u4e2a\u5217\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>SELECT DISTINCT name\nFROM users;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u6b64\u67e5\u8be2\u5c06\u4ece users \u8868\u4e2d\u9009\u62e9\u552f\u4e00\u7684\u540d\u5b57\u3002<\/p>\n<p><strong>\u4e0e GROUP BY \u7684\u533a\u522b<\/strong><\/p>\n<p>DISTINCT \u4e0e GROUP BY \u90fd\u7528\u4e8e\u53bb\u9664\u91cd\u590d\u503c\uff0c\u4f46\u5b83\u4eec\u7684\u5de5\u4f5c\u65b9\u5f0f\u4e0d\u540c\u3002GROUP BY \u6839\u636e\u6307\u5b9a\u5217\u5bf9\u7ed3\u679c\u8fdb\u884c\u5206\u7ec4\uff0c\u7136\u540e\u4e3a\u6bcf\u4e2a\u7ec4\u4fdd\u7559\u4e00\u4e2a\u4ee3\u8868\u884c\u3002\u800c DISTINCT \u5219\u76f4\u63a5\u53bb\u9664\u91cd\u590d\u884c\uff0c\u800c\u4e0d\u8fdb\u884c\u4efb\u4f55\u5206\u7ec4\u3002<\/p>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p><strong>\u8868\uff1aproducts<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>id<\/th>\n<th>name<\/th>\n<th>price<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>MacBook Air<\/td>\n<td>1000<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>iPhone 13<\/td>\n<td>700<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>AirPods<\/td>\n<td>150<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>MacBook Pro<\/td>\n<td>1200<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>iPhone 13<\/td>\n<td>700<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>\u67e5\u8be2\uff1a<\/strong><\/p>\n<pre>SELECT DISTINCT name\nFROM products;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u7ed3\u679c\uff1a<\/strong><\/p>\n<pre>+----------------+\n| name           |\n+----------------+\n| MacBook Air     |\n| iPhone 13       |\n| AirPods         |\n| MacBook Pro     |\n+----------------+<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fsql&nbsp;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>sql distinct \u5173\u952e\u5b57\u901a\u8fc7\u6bd4\u8f83\u6240\u6709\u5217\u503c\u6765\u53bb\u9664\u91cd\u590d\u884c\uff0c\u53ea\u4fdd\u7559\u552f\u4e00\u7684\u884c\u3002\u7528\u6cd5\u5305\u62ec\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u6307\u5b9a\u8981\u9009\u62e9\u7684\u5217\u540d\uff0c\u540e\u8ddf distinct \u5173\u952e\u5b57\u3002\u6307\u5b9a\u8981\u67e5\u8be2\u7684\u8868\u540d\u3002distinct \u53ef\u4ee5\u5e94\u7528\u4e8e\u4e00\u4e2a\u6216\u591a\u4e2a\u5217\u3002\u4e0e group by \u4e0d\u540c\uff0cdistinct \u76f4\u63a5\u53bb\u9664\u91cd\u590d\u884c\u800c\u4e0d\u8fdb\u884c\u5206\u7ec4\u3002 SQL DISTINCT\uff1a\u53bb\u9664\u91cd\u590d\u503c SQL DISTINCT \u5173\u952e\u5b57\u7528\u4e8e\u4ece\u67e5\u8be2\u7ed3\u679c\u4e2d\u53bb\u9664\u91cd\u590d\u884c\uff0c\u53ea\u4fdd\u7559\u552f\u4e00\u7684\u884c\u3002\u5b83\u901a\u8fc7\u6bd4\u8f83\u6bcf\u4e00\u884c\u7684\u6240\u6709\u5217\u503c\u6765\u786e\u5b9a\u552f\u4e00\u6027\u3002 \u8bed\u6cd5 SELECT DISTINCT column_1, column_2, &#8230; FROM table_name; \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff1a column_1, column_2, &#8230;\uff1a\u8981\u9009\u62e9\u7684\u5217\u540d table_name\uff1a\u8981\u67e5\u8be2\u7684\u8868\u540d \u7528\u6cd5 DISTINCT \u5173\u952e\u5b57\u53ef\u4ee5\u5e94\u7528\u4e8e\u4e00\u4e2a\u6216\u591a\u4e2a\u5217\u3002\u4f8b\u5982\uff1a SELECT DISTINCT name FROM users; \u767b\u5f55\u540e\u590d\u5236 \u6b64\u67e5\u8be2\u5c06\u4ece users \u8868\u4e2d\u9009\u62e9\u552f\u4e00\u7684\u540d\u5b57\u3002 \u4e0e GROUP BY \u7684\u533a\u522b DISTINCT \u4e0e GROUP BY \u90fd\u7528\u4e8e\u53bb\u9664\u91cd\u590d\u503c\uff0c\u4f46\u5b83\u4eec\u7684\u5de5\u4f5c\u65b9\u5f0f\u4e0d\u540c\u3002GROUP BY \u6839\u636e\u6307\u5b9a\u5217\u5bf9\u7ed3\u679c\u8fdb\u884c\u5206\u7ec4\uff0c\u7136\u540e\u4e3a\u6bcf\u4e2a\u7ec4\u4fdd\u7559\u4e00\u4e2a\u4ee3\u8868\u884c\u3002\u800c DISTINCT \u5219\u76f4\u63a5\u53bb\u9664\u91cd\u590d\u884c\uff0c\u800c\u4e0d\u8fdb\u884c\u4efb\u4f55\u5206\u7ec4\u3002 \u793a\u4f8b [&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-5964","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5964","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=5964"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5964\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5964"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5964"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5964"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}