{"id":5888,"date":"2024-11-14T09:00:55","date_gmt":"2024-11-14T01:00:55","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5888\/"},"modified":"2024-11-14T09:00:55","modified_gmt":"2024-11-14T01:00:55","slug":"sql%e6%80%8e%e4%b9%88%e5%8e%bb%e9%87%8d%e5%a4%8d","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5888\/","title":{"rendered":"sql\u600e\u4e48\u53bb\u91cd\u590d"},"content":{"rendered":"<blockquote><p>\n  sql \u4e2d\u53bb\u91cd\u590d\u662f\u6307\u5220\u9664\u91cd\u590d\u8bb0\u5f55\uff0c\u4fdd\u7559\u552f\u4e00\u503c\u3002\u53ef\u4f7f\u7528 distinct \u5173\u952e\u5b57\u6216 group by \u5b50\u53e5\u5b9e\u73b0\u53bb\u91cd\u590d\uff1a1. distinct \u5173\u952e\u5b57\uff1a\u8fd4\u56de\u552f\u4e00\u503c\uff0c\u4ec5\u4fdd\u7559\u4e0d\u540c\u503c\uff1b2. group by \u5b50\u53e5\uff1a\u5c06\u76f8\u540c\u503c\u5206\u7ec4\uff0c\u4fdd\u7559\u6bcf\u4e2a\u7ec4\u4e00\u4e2a\u4ee3\u8868\u503c\uff0c\u5e76\u53ef\u8ba1\u7b97\u805a\u5408\u51fd\u6570\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/03\/2024060321452160178.jpg\" class=\"aligncenter\" title=\"sql\u600e\u4e48\u53bb\u91cd\u590d\u63d2\u56fe\" alt=\"sql\u600e\u4e48\u53bb\u91cd\u590d\u63d2\u56fe\" \/><\/p>\n<p><strong>SQL \u5982\u4f55\u53bb\u91cd\u590d<\/strong><\/p>\n<p><strong>\u7b80\u8981\u56de\u7b54\uff1a<\/strong><\/p>\n<p>SQL \u4e2d\u53bb\u91cd\u590d\u662f\u6307\u5220\u9664\u91cd\u590d\u8bb0\u5f55\uff0c\u4fdd\u7559\u552f\u4e00\u503c\u3002\u53ef\u4ee5\u4f7f\u7528 DISTINCT \u5173\u952e\u5b57\u6216 GROUP BY \u5b50\u53e5\u6765\u5b9e\u73b0\u53bb\u91cd\u590d\u3002<\/p>\n<p><strong>\u8be6\u7ec6\u89e3\u91ca\uff1a<\/strong><\/p>\n<p><strong>DISTINCT \u5173\u952e\u5b57<\/strong><\/p>\n<p>DISTINCT \u5173\u952e\u5b57\u7528\u4e8e\u4ece\u67e5\u8be2\u7ed3\u679c\u4e2d\u8fd4\u56de\u552f\u4e00\u503c\u3002\u5b83\u53ef\u4ee5\u4e0e SELECT \u8bed\u53e5\u4e00\u8d77\u4f7f\u7528\uff0c\u540e\u8ddf\u8981\u7b5b\u9009\u7684\u5217\u540d\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>SELECT DISTINCT column_name\nFROM table_name;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>GROUP BY \u5b50\u53e5<\/strong><\/p>\n<p>GROUP BY \u5b50\u53e5\u5c06\u5177\u6709\u76f8\u540c\u503c\u7684\u884c\u5206\u7ec4\uff0c\u7136\u540e\u4ec5\u8fd4\u56de\u6bcf\u4e2a\u7ec4\u7684\u4e00\u4e2a\u4ee3\u8868\u884c\u3002\u5b83\u53ef\u4ee5\u7528\u4e8e\u8ba1\u7b97\uff08\u5982 COUNT()\u3001SUM() \u7b49\uff09\u5e76\u53bb\u91cd\u590d\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>SELECT column_name, COUNT(*) AS count\nFROM table_name\nGROUP BY column_name;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u9009\u62e9\u65b9\u6cd5<\/strong><\/p>\n<p>\u9009\u62e9\u53bb\u91cd\u590d\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u9700\u6c42\uff1a<\/p>\n<ul>\n<li>\u5982\u679c\u53ea\u5173\u5fc3\u4fdd\u7559\u552f\u4e00\u503c\uff0c\u5219\u4f7f\u7528 DISTINCT \u5173\u952e\u5b57\u3002<\/li>\n<li>\u5982\u679c\u9700\u8981\u8ba1\u7b97\u805a\u5408\u51fd\u6570\u5e76\u53bb\u91cd\u590d\uff0c\u5219\u4f7f\u7528 GROUP BY \u5b50\u53e5\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u8003\u8651\u4ee5\u4e0b\u8868 customers\uff0c\u5176\u4e2d\u5305\u542b\u91cd\u590d\u7684\u5ba2\u6237\u59d3\u540d\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>customer_id<\/th>\n<th>customer_name<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>John Smith<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>Jane Doe<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>John Smith<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>Mary Johnson<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>\u4f7f\u7528 DISTINCT \u53bb\u91cd\u590d\uff1a<\/strong><\/p>\n<pre>SELECT DISTINCT customer_name\nFROM customers;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u7ed3\u679c\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>customer_name<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>John Smith<\/td>\n<\/tr>\n<tr>\n<td>Jane Doe<\/td>\n<\/tr>\n<tr>\n<td>Mary Johnson<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>\u4f7f\u7528 GROUP BY \u53bb\u91cd\u590d\u5e76\u8ba1\u7b97\u8bb0\u5f55\u6570\uff1a<\/strong><\/p>\n<pre>SELECT customer_name, COUNT(*) AS count\nFROM customers\nGROUP BY customer_name;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u7ed3\u679c\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>customer_name<\/th>\n<th>count<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>John Smith<\/td>\n<td>2<\/td>\n<\/tr>\n<tr>\n<td>Jane Doe<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>Mary Johnson<\/td>\n<td>1<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4ee5\u4e0a\u5c31\u662fsql\u600e\u4e48\u53bb\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\u53bb\u91cd\u590d\u662f\u6307\u5220\u9664\u91cd\u590d\u8bb0\u5f55\uff0c\u4fdd\u7559\u552f\u4e00\u503c\u3002\u53ef\u4f7f\u7528 distinct \u5173\u952e\u5b57\u6216 group by \u5b50\u53e5\u5b9e\u73b0\u53bb\u91cd\u590d\uff1a1. distinct \u5173\u952e\u5b57\uff1a\u8fd4\u56de\u552f\u4e00\u503c\uff0c\u4ec5\u4fdd\u7559\u4e0d\u540c\u503c\uff1b2. group by \u5b50\u53e5\uff1a\u5c06\u76f8\u540c\u503c\u5206\u7ec4\uff0c\u4fdd\u7559\u6bcf\u4e2a\u7ec4\u4e00\u4e2a\u4ee3\u8868\u503c\uff0c\u5e76\u53ef\u8ba1\u7b97\u805a\u5408\u51fd\u6570\u3002 SQL \u5982\u4f55\u53bb\u91cd\u590d \u7b80\u8981\u56de\u7b54\uff1a SQL \u4e2d\u53bb\u91cd\u590d\u662f\u6307\u5220\u9664\u91cd\u590d\u8bb0\u5f55\uff0c\u4fdd\u7559\u552f\u4e00\u503c\u3002\u53ef\u4ee5\u4f7f\u7528 DISTINCT \u5173\u952e\u5b57\u6216 GROUP BY \u5b50\u53e5\u6765\u5b9e\u73b0\u53bb\u91cd\u590d\u3002 \u8be6\u7ec6\u89e3\u91ca\uff1a DISTINCT \u5173\u952e\u5b57 DISTINCT \u5173\u952e\u5b57\u7528\u4e8e\u4ece\u67e5\u8be2\u7ed3\u679c\u4e2d\u8fd4\u56de\u552f\u4e00\u503c\u3002\u5b83\u53ef\u4ee5\u4e0e SELECT \u8bed\u53e5\u4e00\u8d77\u4f7f\u7528\uff0c\u540e\u8ddf\u8981\u7b5b\u9009\u7684\u5217\u540d\u3002\u4f8b\u5982\uff1a SELECT DISTINCT column_name FROM table_name; \u767b\u5f55\u540e\u590d\u5236 GROUP BY \u5b50\u53e5 GROUP BY \u5b50\u53e5\u5c06\u5177\u6709\u76f8\u540c\u503c\u7684\u884c\u5206\u7ec4\uff0c\u7136\u540e\u4ec5\u8fd4\u56de\u6bcf\u4e2a\u7ec4\u7684\u4e00\u4e2a\u4ee3\u8868\u884c\u3002\u5b83\u53ef\u4ee5\u7528\u4e8e\u8ba1\u7b97\uff08\u5982 COUNT()\u3001SUM() \u7b49\uff09\u5e76\u53bb\u91cd\u590d\u3002\u4f8b\u5982\uff1a SELECT column_name, COUNT(*) AS count FROM table_name GROUP BY column_name; \u767b\u5f55\u540e\u590d\u5236 \u9009\u62e9\u65b9\u6cd5 [&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-5888","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5888","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=5888"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5888\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5888"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5888"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5888"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}