{"id":5203,"date":"2024-11-14T08:04:05","date_gmt":"2024-11-14T00:04:05","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5203\/"},"modified":"2024-11-14T08:04:05","modified_gmt":"2024-11-14T00:04:05","slug":"%e5%8e%bb%e9%87%8d%e5%9c%a8sql%e4%b8%ad%e7%9a%84%e7%94%a8%e6%b3%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5203\/","title":{"rendered":"\u53bb\u91cd\u5728sql\u4e2d\u7684\u7528\u6cd5"},"content":{"rendered":"<blockquote><p>\n  \u5728 sql \u4e2d\uff0c\u53bb\u91cd\u53ef\u79fb\u9664\u6570\u636e\u96c6\u4e2d\u7684\u91cd\u590d\u503c\uff0c\u786e\u4fdd\u6570\u636e\u5b8c\u6574\u6027\u5e76\u63d0\u9ad8\u67e5\u8be2\u6027\u80fd\u3002\u5e38\u7528\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528 distinct \u5173\u952e\u5b57\u4ece\u6307\u5b9a\u5217\u9009\u62e9\u552f\u4e00\u503c\uff1b\u4f7f\u7528 group by \u5b50\u53e5\u6309\u6307\u5b9a\u5217\u5206\u7ec4\u5e76\u8fd4\u56de\u805a\u5408\u503c\uff1b\u4f7f\u7528 union \u8fd0\u7b97\u7b26\u5408\u5e76\u67e5\u8be2\u7ed3\u679c\u5e76\u79fb\u9664\u91cd\u590d\u9879\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/24\/2024052420272071039.jpg\" class=\"aligncenter\" title=\"\u53bb\u91cd\u5728sql\u4e2d\u7684\u7528\u6cd5\u63d2\u56fe\" alt=\"\u53bb\u91cd\u5728sql\u4e2d\u7684\u7528\u6cd5\u63d2\u56fe\" \/><\/p>\n<p><strong>\u53bb\u91cd\u5728 SQL \u4e2d\u7684\u7528\u6cd5<\/strong><\/p>\n<p>\u5728 SQL \u4e2d\uff0c\u53bb\u91cd\u662f\u6307\u4ece\u4e00\u4e2a\u6570\u636e\u96c6\u4e2d\u79fb\u9664\u91cd\u590d\u503c\u3002\u8fd9\u53ef\u4ee5\u7528\u4e8e\u6e05\u7406\u6570\u636e\u3001\u786e\u4fdd\u6570\u636e\u5b8c\u6574\u6027\uff0c\u4ee5\u53ca\u63d0\u9ad8\u67e5\u8be2\u6027\u80fd\u3002<\/p>\n<p><strong>DISTINCT \u5173\u952e\u5b57<\/strong><\/p>\n<p>\u6700\u5e38\u7528\u7684\u53bb\u91cd\u65b9\u6cd5\u662f\u4f7f\u7528 DISTINCT \u5173\u952e\u5b57\u3002DISTINCT \u5173\u952e\u5b57\u653e\u7f6e\u5728 SELECT \u8bed\u53e5\u4e2d\uff0c\u540e\u8ddf\u8981\u7b5b\u9009\u7684\u5217\u6216\u8868\u8fbe\u5f0f\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>\u8fd9\u5c06\u4ece table_name \u8868\u7684 column_name \u5217\u4e2d\u9009\u62e9\u552f\u4e00\u503c\u3002<\/p>\n<p><strong>GROUP BY \u5b50\u53e5<\/strong><\/p>\n<p>GROUP BY \u5b50\u53e5\u4e5f\u53ef\u4ee5\u7528\u4e8e\u53bb\u91cd\u3002GROUP BY \u5c06\u8868\u4e2d\u7684\u884c\u5206\u7ec4\uff0c\u7136\u540e\u4e3a\u6bcf\u4e2a\u7ec4\u8fd4\u56de\u4e00\u4e2a\u805a\u5408\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>SELECT column_name\nFROM table_name\nGROUP BY column_name;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u5c06\u4ece table_name \u8868\u7684 column_name \u5217\u4e2d\u9009\u62e9\u552f\u4e00\u503c\uff0c\u5e76\u4e3a\u6bcf\u4e2a\u552f\u4e00\u503c\u8fd4\u56de\u4e00\u884c\u3002<\/p>\n<p><strong>UNION \u8fd0\u7b97\u7b26<\/strong><\/p>\n<p>UNION \u8fd0\u7b97\u7b26\u53ef\u4ee5\u5c06\u4e24\u4e2a\u6216\u591a\u4e2a\u67e5\u8be2\u7684\u7ed3\u679c\u5408\u5e76\u5230\u4e00\u4e2a\u8868\u4e2d\u3002\u5982\u679c\u7ed3\u679c\u96c6\u4e2d\u5305\u542b\u91cd\u590d\u503c\uff0cUNION \u8fd0\u7b97\u7b26\u4f1a\u81ea\u52a8\u5c06\u5176\u5220\u9664\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>SELECT column_name\nFROM table1\nUNION\nSELECT column_name\nFROM table2;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u5c06\u4ece table1 \u548c table2 \u8868\u4e2d\u9009\u62e9 column_name \u5217\u7684\u552f\u4e00\u503c\u3002<\/p>\n<p><strong>\u5176\u4ed6\u53bb\u91cd\u65b9\u6cd5<\/strong><\/p>\n<p>\u9664\u4e86\u4e0a\u9762\u63d0\u5230\u7684\u65b9\u6cd5\u4e4b\u5916\uff0c\u8fd8\u6709\u5176\u4ed6\u65b9\u6cd5\u4e5f\u53ef\u4ee5\u7528\u4e8e\u53bb\u91cd\uff0c\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528 INTERSECT \u548c EXCEPT \u8fd0\u7b97\u7b26<\/li>\n<li>\u4f7f\u7528\u5b50\u67e5\u8be2<\/li>\n<li>\u521b\u5efa\u552f\u4e00\u7d22\u5f15<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u53bb\u91cd\u5728sql\u4e2d\u7684\u7528\u6cd5\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>\u5728 sql \u4e2d\uff0c\u53bb\u91cd\u53ef\u79fb\u9664\u6570\u636e\u96c6\u4e2d\u7684\u91cd\u590d\u503c\uff0c\u786e\u4fdd\u6570\u636e\u5b8c\u6574\u6027\u5e76\u63d0\u9ad8\u67e5\u8be2\u6027\u80fd\u3002\u5e38\u7528\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528 distinct \u5173\u952e\u5b57\u4ece\u6307\u5b9a\u5217\u9009\u62e9\u552f\u4e00\u503c\uff1b\u4f7f\u7528 group by \u5b50\u53e5\u6309\u6307\u5b9a\u5217\u5206\u7ec4\u5e76\u8fd4\u56de\u805a\u5408\u503c\uff1b\u4f7f\u7528 union \u8fd0\u7b97\u7b26\u5408\u5e76\u67e5\u8be2\u7ed3\u679c\u5e76\u79fb\u9664\u91cd\u590d\u9879\u3002 \u53bb\u91cd\u5728 SQL \u4e2d\u7684\u7528\u6cd5 \u5728 SQL \u4e2d\uff0c\u53bb\u91cd\u662f\u6307\u4ece\u4e00\u4e2a\u6570\u636e\u96c6\u4e2d\u79fb\u9664\u91cd\u590d\u503c\u3002\u8fd9\u53ef\u4ee5\u7528\u4e8e\u6e05\u7406\u6570\u636e\u3001\u786e\u4fdd\u6570\u636e\u5b8c\u6574\u6027\uff0c\u4ee5\u53ca\u63d0\u9ad8\u67e5\u8be2\u6027\u80fd\u3002 DISTINCT \u5173\u952e\u5b57 \u6700\u5e38\u7528\u7684\u53bb\u91cd\u65b9\u6cd5\u662f\u4f7f\u7528 DISTINCT \u5173\u952e\u5b57\u3002DISTINCT \u5173\u952e\u5b57\u653e\u7f6e\u5728 SELECT \u8bed\u53e5\u4e2d\uff0c\u540e\u8ddf\u8981\u7b5b\u9009\u7684\u5217\u6216\u8868\u8fbe\u5f0f\u3002\u4f8b\u5982\uff1a SELECT DISTINCT column_name FROM table_name; \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u5c06\u4ece table_name \u8868\u7684 column_name \u5217\u4e2d\u9009\u62e9\u552f\u4e00\u503c\u3002 GROUP BY \u5b50\u53e5 GROUP BY \u5b50\u53e5\u4e5f\u53ef\u4ee5\u7528\u4e8e\u53bb\u91cd\u3002GROUP BY \u5c06\u8868\u4e2d\u7684\u884c\u5206\u7ec4\uff0c\u7136\u540e\u4e3a\u6bcf\u4e2a\u7ec4\u8fd4\u56de\u4e00\u4e2a\u805a\u5408\u503c\u3002\u4f8b\u5982\uff1a SELECT column_name FROM table_name GROUP BY column_name; \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u5c06\u4ece table_name \u8868\u7684 column_name [&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-5203","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5203","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=5203"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5203\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}