{"id":1503,"date":"2026-07-16T07:17:21","date_gmt":"2026-07-15T23:17:21","guid":{"rendered":"https:\/\/www.loongdi.net\/blog\/?p=1503"},"modified":"2026-07-16T07:17:21","modified_gmt":"2026-07-15T23:17:21","slug":"sql%e5%8e%bb%e9%87%8d%e5%a6%82%e4%bd%95%e6%8f%90%e9%ab%98%e6%95%b0%e6%8d%ae%e6%9f%a5%e8%af%a2%e6%95%88%e7%8e%87%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.loongdi.net\/blog\/1503.html","title":{"rendered":"SQL\u53bb\u91cd\u5982\u4f55\u63d0\u9ad8\u6570\u636e\u67e5\u8be2\u6548\u7387\uff1f"},"content":{"rendered":"<p><h3>\u4ec0\u4e48\u662fSQL\u53bb\u91cd\uff1f<\/h3>\n<\/p>\n<p><p>SQL\u53bb\u91cd\u662f\u6307\u5728\u6570\u636e\u5e93\u67e5\u8be2\u8fc7\u7a0b\u4e2d\uff0c\u53bb\u9664\u67e5\u8be2\u7ed3\u679c\u4e2d\u91cd\u590d\u7684\u6570\u636e\u8bb0\u5f55\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u83b7\u53d6\u552f\u4e00\u7684\u6570\u636e\u96c6\uff0c\u907f\u514d\u6570\u636e\u5197\u4f59\uff0c\u63d0\u9ad8\u6570\u636e\u67e5\u8be2\u7684\u6548\u7387\u548c\u51c6\u786e\u6027\u3002<\/p>\n<\/p>\n<p><h3>SQL\u53bb\u91cd\u5305\u542b\u54ea\u4e9b\u5185\u5bb9\uff1f<\/h3>\n<\/p>\n<p><p>SQL\u53bb\u91cd\u4e3b\u8981\u5305\u62ec\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u9762\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u53bb\u91cd\u5b57\u6bb5\uff1a\u6307\u5b9a\u9700\u8981\u53bb\u91cd\u7684\u5b57\u6bb5\u3002<\/li>\n<li>\u53bb\u91cd\u6761\u4ef6\uff1a\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u8fdb\u884c\u53bb\u91cd\u3002<\/li>\n<li>\u67e5\u8be2\u7ed3\u679c\uff1a\u8fd4\u56de\u53bb\u91cd\u540e\u7684\u6570\u636e\u96c6\u3002<\/li>\n<\/ul>\n<p><h3>SQL\u53bb\u91cd\u5e38\u7528\u5173\u952e\u5b57<\/h3>\n<\/p>\n<p><p>\u5728SQL\u8bed\u53e5\u4e2d\uff0c\u5e38\u7528\u7684\u53bb\u91cd\u5173\u952e\u5b57\u6709\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>DISTINCT<\/code>\uff1a\u8fd4\u56de\u552f\u4e00\u7684\u6570\u636e\u96c6\u3002<\/li>\n<li><code>GROUP BY<\/code>\uff1a\u6839\u636e\u6307\u5b9a\u5b57\u6bb5\u5206\u7ec4\uff0c\u5e38\u4e0e<code>COUNT<\/code>\u7b49\u51fd\u6570\u4e00\u8d77\u4f7f\u7528\u3002<\/li>\n<li><code>HAVING<\/code>\uff1a\u5bf9\u5206\u7ec4\u540e\u7684\u7ed3\u679c\u8fdb\u884c\u8fc7\u6ee4\u3002<\/li>\n<\/ul>\n<p><h3>\u5982\u4f55\u8fdb\u884cSQL\u53bb\u91cd\uff1f<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u51e0\u79cd\u5e38\u89c1\u7684SQL\u53bb\u91cd\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code>SELECT DISTINCT column_name<\/p>\r\n<p>FROM table_name;<\/p>\r\n\r\n<p>SELECT column_name<\/p>\r\n<p>FROM table_name<\/p>\r\n<p>GROUP BY column_name;<\/p>\r\n\r\n<p>SELECT column_name<\/p>\r\n<p>FROM table_name<\/p>\r\n<p>WHERE column_name IN (<\/p>\r\n<p>  SELECT DISTINCT column_name<\/p>\r\n<p>  FROM table_name<\/p>\r\n<p>);<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p><h3>SQL\u53bb\u91cd\u63a8\u8350\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e9b\u63a8\u8350\u7684SQL\u53bb\u91cd\u7528\u6cd5\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u5f53\u67e5\u8be2\u7ed3\u679c\u4e2d\u5305\u542b\u91cd\u590d\u6570\u636e\u65f6\uff0c\u4f7f\u7528<code>DISTINCT<\/code>\u5173\u952e\u5b57\u8fdb\u884c\u53bb\u91cd\u3002<\/li>\n<li>\u5f53\u9700\u8981\u6309\u7167\u67d0\u4e2a\u5b57\u6bb5\u5206\u7ec4\uff0c\u5e76\u8ba1\u7b97\u6bcf\u4e2a\u7ec4\u7684\u6570\u91cf\u65f6\uff0c\u4f7f\u7528<code>GROUP BY<\/code>\u548c<code>COUNT<\/code>\u51fd\u6570\u3002<\/li>\n<li>\u5f53\u9700\u8981\u5bf9\u5206\u7ec4\u540e\u7684\u7ed3\u679c\u8fdb\u884c\u8fc7\u6ee4\u65f6\uff0c\u4f7f\u7528<code>HAVING<\/code>\u5173\u952e\u5b57\u3002<\/li>\n<\/ul>\n<p><h3>\u4e3a\u4ec0\u4e48\u4f7f\u7528SQL\u53bb\u91cd\uff1f<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528SQL\u53bb\u91cd\u7684\u539f\u56e0\u6709\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u63d0\u9ad8\u67e5\u8be2\u6548\u7387\uff1a\u901a\u8fc7\u53bb\u9664\u91cd\u590d\u6570\u636e\uff0c\u53ef\u4ee5\u51cf\u5c11\u67e5\u8be2\u7684\u6570\u636e\u91cf\uff0c\u63d0\u9ad8\u67e5\u8be2\u6548\u7387\u3002<\/li>\n<li>\u4fdd\u8bc1\u6570\u636e\u51c6\u786e\u6027\uff1a\u907f\u514d\u91cd\u590d\u6570\u636e\u5bfc\u81f4\u7684\u6570\u636e\u7edf\u8ba1\u9519\u8bef\u3002<\/li>\n<li>\u7b80\u5316\u6570\u636e\u5904\u7406\uff1a\u4f7f\u6570\u636e\u5904\u7406\u66f4\u52a0\u7b80\u5355\uff0c\u6613\u4e8e\u7ef4\u62a4\u3002<\/li>\n<\/ul>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.loongdi.net\/blog\/wp-content\/uploads\/2026\/03\/F5d3lgqYLJ.jpg\" alt=\"SQL\u53bb\u91cd\u5982\u4f55\u63d0\u9ad8\u6570\u636e\u67e5\u8be2\u6548\u7387\uff1f\" title=\"SQL\u53bb\u91cd\u5982\u4f55\u63d0\u9ad8\u6570\u636e\u67e5\u8be2\u6548\u7387\uff1f\"><\/p>\n<p><h3>\u95ee\u7b54\u73af\u8282<\/h3>\n<\/p>\n<p><b>1. \u4e3a\u4ec0\u4e48\u8981\u4f7f\u7528<code>DISTINCT<\/code>\u5173\u952e\u5b57\u8fdb\u884cSQL\u53bb\u91cd\uff1f<\/b><\/p>\n<p><p><code>DISTINCT<\/code>\u5173\u952e\u5b57\u53ef\u4ee5\u786e\u4fdd\u67e5\u8be2\u7ed3\u679c\u4e2d\u4e0d\u5305\u542b\u91cd\u590d\u7684\u6570\u636e\u8bb0\u5f55\uff0c\u63d0\u9ad8\u6570\u636e\u7684\u51c6\u786e\u6027\u3002<\/p>\n<\/p>\n<p><b>2. \u5728\u4ec0\u4e48\u60c5\u51b5\u4e0b\u5e94\u8be5\u4f7f\u7528<code>GROUP BY<\/code>\u548c<code>COUNT<\/code>\u51fd\u6570\u8fdb\u884cSQL\u53bb\u91cd\uff1f<\/b><\/p>\n<p><p>\u5f53\u9700\u8981\u6839\u636e\u67d0\u4e2a\u5b57\u6bb5\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u7ec4\uff0c\u5e76\u7edf\u8ba1\u6bcf\u4e2a\u7ec4\u7684\u6570\u91cf\u65f6\uff0c\u5e94\u8be5\u4f7f\u7528<code>GROUP BY<\/code>\u548c<code>COUNT<\/code>\u51fd\u6570\u8fdb\u884cSQL\u53bb\u91cd\u3002<\/p>\n<\/p>\n<p><b>3. \u5728SQL\u53bb\u91cd\u4e2d\uff0c\u5982\u4f55\u6392\u9664\u67d0\u4e2a\u5b57\u6bb5\u7684\u91cd\u590d\u8bb0\u5f55\uff1f<\/b><\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u9700\u8981\u53bb\u91cd\u7684\u5b57\u6bb5\uff0c\u5e76\u4f7f\u7528<code>DISTINCT<\/code>\u5173\u952e\u5b57\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code>SELECT DISTINCT column1, column2<\/p>\r\n<p>FROM table_name;<\/p>\r\n<p><\/code><\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ec0\u4e48\u662fSQL\u53bb\u91cd\uff1f SQL\u53bb\u91cd\u662f\u6307\u5728\u6570\u636e\u5e93\u67e5\u8be2\u8fc7\u7a0b\u4e2d\uff0c\u53bb\u9664\u67e5\u8be2\u7ed3\u679c\u4e2d\u91cd\u590d\u7684\u6570\u636e\u8bb0\u5f55\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u83b7\u53d6\u552f\u4e00\u7684\u6570\u636e\u96c6\uff0c\u907f\u514d\u6570\u636e\u5197\u4f59\uff0c\u63d0\u9ad8\u6570\u636e\u67e5\u8be2\u7684\u6548\u7387\u548c\u51c6\u786e\u6027\u3002 SQL\u53bb\u91cd\u5305\u542b\u54ea\u4e9b\u5185\u5bb9\uff1f SQL\u53bb\u91cd\u4e3b\u8981\u5305\u62ec\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u9762\uff1a \u53bb\u91cd\u5b57\u6bb5\uff1a\u6307\u5b9a&#8230;<\/p>\n","protected":false},"author":1,"featured_media":145,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"topic":[],"class_list":["post-1503","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-yun"],"_links":{"self":[{"href":"https:\/\/www.loongdi.net\/blog\/wp-json\/wp\/v2\/posts\/1503","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.loongdi.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.loongdi.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.loongdi.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.loongdi.net\/blog\/wp-json\/wp\/v2\/comments?post=1503"}],"version-history":[{"count":1,"href":"https:\/\/www.loongdi.net\/blog\/wp-json\/wp\/v2\/posts\/1503\/revisions"}],"predecessor-version":[{"id":1504,"href":"https:\/\/www.loongdi.net\/blog\/wp-json\/wp\/v2\/posts\/1503\/revisions\/1504"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.loongdi.net\/blog\/wp-json\/wp\/v2\/media\/145"}],"wp:attachment":[{"href":"https:\/\/www.loongdi.net\/blog\/wp-json\/wp\/v2\/media?parent=1503"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.loongdi.net\/blog\/wp-json\/wp\/v2\/categories?post=1503"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.loongdi.net\/blog\/wp-json\/wp\/v2\/tags?post=1503"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.loongdi.net\/blog\/wp-json\/wp\/v2\/topic?post=1503"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}